擅长:python、mysql、java
<p>它总是指向<code>finally</code>块,因此它将忽略<code>try</code>和<code>except</code>中的<code>return</code>。如果在<code>try</code>和<code>except</code>之上有一个<code>return</code>,它将返回该值。</p>
<pre><code>def func1():
try:
return 1 # ignoring the return
finally:
return 2 # returns this return
def func2():
try:
raise ValueError()
except:
# is going to this exception block, but ignores the return because it needs to go to the finally
return 1
finally:
return 3
def func3():
return 0 # finds a return here, before the try except and finally block, so it will use this return
try:
raise ValueError()
except:
return 1
finally:
return 3
func1() # returns 2
func2() # returns 3
func3() # returns 0
</code></pre>