擅长:python、mysql、java
<p>来自Python<a href="https://docs.python.org/3/tutorial/errors.html" rel="noreferrer">documentation</a></p>
<blockquote>
<p>A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in a except or else clause), it is re-raised after the finally clause has been executed. The finally clause <strong>is also executed “on the way out” when any other clause of the try statement is left via a break, continue or return statement</strong>. A more complicated example (having except and finally clauses in the same try statement works as of Python 2.5):</p>
</blockquote>
<p>因此,一旦try/except块使用<strong>return</strong>离开,它会将返回值设置为给定的-finally块将始终<strong>execute,并应用于释放资源等,而在那里使用另一个return-覆盖原始块。</p>
<p>在您的特定情况下,<code>func1()</code>返回<code>2</code>和<code>func2()</code>返回<code>3</code>,因为这些是finally块中返回的值。</p>