【发布时间】:2020-06-26 12:34:05
【问题描述】:
所以我有两个不同的 try-except 块,我不理解输出,我相信这是因为 except 块中的异常。尽管我发现了一些标题相似的问题,但它们并没有帮助我回答我的问题。
第一块:
try:
try:
raise IndexError
x = 5/0
except ArithmeticError:
print("1")
print("2")
except IndexError:
print("3")
finally:
print("4")
except:
print("5") #Output: 3 4
既然我们捕获了IndexError,为什么最后一个异常5?
(我确实理解 raise IndexError 被第二个捕获,除了我们得到 3,因为 finally 总是被执行,4 也被打印出来了)。
第二个(相关)问题:
try:
try:
x = 5/0
except ArithmeticError:
print("1")
raise IndexError # this is different this time!
print("2")
except IndexError:
print("3")
finally:
print("4")
except:
print("5") #Output: 1 4 5
raise IndexError 不执行print("3") 语句是怎么回事?既然我们在第一个示例中没有得到它,为什么这次我们得到了 5 输出?
【问题讨论】:
标签: python-3.x exception error-handling try-catch