def fancy_divide(list_of_numbers, index):
try:
try:
raise Exception("0")
finally:
denom = list_of_numbers[index]
for i in range(len(list_of_numbers)):
list_of_numbers[i] /= denom
except Exception as ex:
print(ex)
调用fancy_divide([0,2,4],0)时,此代码是否打印0?
答案 0 :(得分:1)
它不会打印0
,因为finally
块中引发了另一个异常,从而替换了原始异常Exception("0")
,因此外部try-except
块仅打印了后者