擅长:python、mysql、java
<p>从<a href="https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions" rel="nofollow">documentation</a>:</p>
<blockquote>
<p>A finally clause is <strong>always executed</strong> <strong>before leaving</strong> the try statement, whether an exception has occurred or not. [...] The finally clause is also <strong>executed “on the way out”</strong> when any other clause of the try statement is left via a break, continue or <strong>return statement</strong>.</p>
</blockquote>
<p>也就是说,在一个函数中,finally子句总是返回</em>。即使没有发生异常:</p>
<pre><code>def f():
try:
return 'OK'
finally:
return 'Finally'
f() # returns 'Finally'
</code></pre>