可以使用带有一个except语句的try和except块捕获异常吗?

时间:2015-05-06 14:45:29

标签: python python-2.7 exception-handling

在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

0 个答案:

没有答案