对于 Python 中任何可能的 try-finally 块,是否保证始终执行 finally块?
例如,假设我在 except块中返回:
try:
1/0
except ZeroDivisionError:
return
finally:
print("Does this code run?")
或者我可以重新提高 Exception:
try:
1/0
except ZeroDivisionError:
raise
finally:
print("What about this code?")
测试显示 finally确实在上面的例子中得到了执行,但是我想还有其他我没有想到的场景。
是否存在 finally块在 Python 中执行失败的情况?