我想编写一个装饰器,您可以在其中指定调用函数时要捕获的任意数量的异常(然后是如何处理每个异常的异常输出)。现在,如果我想知道例外的数量,我可以做:
try:
fun()
except SomeException1 as e:
# handle
except SomeException2 as e:
# handle
但是可以说我得到了例外列表。
exceptions_to_handle = [SomeException1, SomeException2]
可能可以做到这一点,但是也许有更好的方法吗?
try:
fun()
except Exception as e:
for exc in exceptions_to_handle:
if isinstance(e, exc):
# handle