如何在 Exception/Error 的情况下不停止 python 中其他函数的执行
How not to stop the execution of other function in python in case of Exception/Error
我在 python 中有一个脚本,其工作方式如下所示。每个函数执行完全不同的任务并且彼此不相关。我的问题是,如果 function2() 在执行过程中出现问题,那么 function3(), function4(), function5() 不会执行。我知道你会说通过捕获异常来处理这个问题(try..except)但是我必须捕获所有不是我正在寻找的异常。简而言之,如果任何功能出现问题,我该如何编码而不影响我的其他功能。理想情况下,它应该排除那个有问题的函数并让另一个函数执行。
def function1():
some code
def function2():
some code
def function3():
some code
def function4():
some code
def function5():
some code
if __name__ == '__main__':
function1()
function2()
function3()
function4()
function5()
不需要写多个try/except
。创建一个函数列表并执行它们。例如,你的代码应该是这样的:
if __name__ == '__main__':
func_list = [function1, function2, function3, function4, function5]
for my_func in func_list:
try:
my_func()
except:
pass
或者,创建一个 装饰器 并将该装饰器添加到您的每个函数中。检查 A guide to Python's function decorators。例如,你的装饰器应该是这样的:
def wrap_error(func):
def func_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
pass
return func_wrapper
现在用你的函数定义添加这个装饰器:
@wrap_error
def function1():
some code
添加了这个装饰器的函数不会引发任何问题Exception
您可以像这样使用异常并捕获所有类型的异常
if __name__ == '__main__':
try:
function1()
except:
pass
try:
function2()
except:
pass
try:
function3()
except:
pass
try:
function4()
except:
pass
您可以使用大量功能
func_dict = {
func1 : {
param1 : val
param2 : val
},
func1 : {
param1 : val
param2 : val
}
}
因此您可以迭代函数字典的键并迭代参数
从 Python 3.4 开始,添加了一个新的上下文管理器 contextlib.suppress
,根据文档:
Return a context manager that suppresses any of the specified exceptions if they occur in the body of a with
statement and then resumes execution with the first statement following the end of the with statement.
为了抑制所有异常,您可以将其用作:
from contextlib import suppress
if __name__ == '__main__':
func_list = [function1, function2, function3, function4, function5]
for my_func in func_list:
with suppress(Exception): # `Exception` to suppress all the exceptions
my_func() # Any exception raised by `my_func()` will be suppressed
我在 python 中有一个脚本,其工作方式如下所示。每个函数执行完全不同的任务并且彼此不相关。我的问题是,如果 function2() 在执行过程中出现问题,那么 function3(), function4(), function5() 不会执行。我知道你会说通过捕获异常来处理这个问题(try..except)但是我必须捕获所有不是我正在寻找的异常。简而言之,如果任何功能出现问题,我该如何编码而不影响我的其他功能。理想情况下,它应该排除那个有问题的函数并让另一个函数执行。
def function1():
some code
def function2():
some code
def function3():
some code
def function4():
some code
def function5():
some code
if __name__ == '__main__':
function1()
function2()
function3()
function4()
function5()
不需要写多个try/except
。创建一个函数列表并执行它们。例如,你的代码应该是这样的:
if __name__ == '__main__':
func_list = [function1, function2, function3, function4, function5]
for my_func in func_list:
try:
my_func()
except:
pass
或者,创建一个 装饰器 并将该装饰器添加到您的每个函数中。检查 A guide to Python's function decorators。例如,你的装饰器应该是这样的:
def wrap_error(func):
def func_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
pass
return func_wrapper
现在用你的函数定义添加这个装饰器:
@wrap_error
def function1():
some code
添加了这个装饰器的函数不会引发任何问题Exception
您可以像这样使用异常并捕获所有类型的异常
if __name__ == '__main__':
try:
function1()
except:
pass
try:
function2()
except:
pass
try:
function3()
except:
pass
try:
function4()
except:
pass
您可以使用大量功能
func_dict = {
func1 : {
param1 : val
param2 : val
},
func1 : {
param1 : val
param2 : val
}
}
因此您可以迭代函数字典的键并迭代参数
从 Python 3.4 开始,添加了一个新的上下文管理器 contextlib.suppress
,根据文档:
Return a context manager that suppresses any of the specified exceptions if they occur in the body of a
with
statement and then resumes execution with the first statement following the end of the with statement.
为了抑制所有异常,您可以将其用作:
from contextlib import suppress
if __name__ == '__main__':
func_list = [function1, function2, function3, function4, function5]
for my_func in func_list:
with suppress(Exception): # `Exception` to suppress all the exceptions
my_func() # Any exception raised by `my_func()` will be suppressed