If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. [..] If the finally clause executes a return or break statement, the saved exception is discarded
A finally clause is always executedbefore leaving the try statement, whether an exception has occurred or not. [...] The finally clause is also executed “on the way out” when any other clause of the try statement is left via a break, continue or return statement.
从docs:
@deceze在他的answer中引用了更相关的部分
函数返回
finally
子句中的字符串,并且在返回后不会引发异常,这就是打印的内容。你知道吗如果您尝试执行:
然后,如您所见,“yes”被打印出来,异常被抛出到print语句之后。你知道吗
这在the documentation中有明确的解释:
从documentation:
也就是说,在一个函数中,finally子句总是返回。即使没有发生异常:
相关问题 更多 >
编程相关推荐