在Python中,可以使用相同的except语句从异常和原始try中捕获异常吗?
例如,在以下代码中:
try:
risky_operation() # This could raise a or b, or it could work
except a:
simple_operation() # This could raise b, or it could work
except b:
guaranteed_operation() # This will always work
如果guaranteed_operation()
提出risky_operation()
,会b
被调用吗?或者我必须这样做:
try:
risky_operation() # This could raise a or b, or it could work
except a:
try:
simple_operation() # This could raise b, or it could work
except b:
guaranteed_operation() # This will always work
except b:
guaranteed_operation() # This will always work