从
https://docs.python.org/2.7/reference/compound_stmts.html#finally:
If finally is present, it specifies a ‘cleanup’ handler. The try clause is
executed, including any except and else clauses. If an exception occurs in
any of the clauses and is not handled, the exception is temporarily saved.
The finally clause is executed. If there is a saved exception, it is
re-raised at the end of the finally clause. If the finally clause raises
another exception or executes a return or break statement, the saved
exception is discarded
这也反映了从PEP341开始之前的最终声明:
这是一个尝试,除了finally块看起来像pre PEP341:
try:
try:
raiseMe()
except:
raise
finally:
#here is where cleanup is supposed to happen before raising error
break
#after finally code: raise error
由于错误的提升永远不会发生在finally块中,所以它从未实际上被提升。
为了保持与python版本2.4及更低版本的向后兼容性,必须以这种方式完成。