我希望能捕获特定异常并相应地处理它,然后继续执行其他异常通用的处理。从C背景下来看,我以前可以使用goto实现所需效果。目前我正在做的是这样的,它可以很好地工作:
try:
output_var = some_magical_function()
except IntegrityError as ie:
integrity_error_handling()
shared_exception_handling_function(zde) # could be error reporting
except SomeOtherException as soe:
shared_exception_handling_function(soe) # the same function as above
简而言之:
是否有“Pythonic”的方法来完成以下操作:
try:
output_var = some_magical_function()
except IntegrityError as ie:
integrity_error_handling()
except ALLExceptions as ae: # all exceptions INCLUDING the IntregityError
shared_exception_handling_function(ae) # could be error reporting
NB: 我知道finally子句- 这不是为了整理(即关闭文件)。
try..except
结构来处理内部的特定异常,然后将其上报到外部以进行一般处理。 - woozyking