Python try finally 块返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19805654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python try finally block returns
提问by skybobbi
There is the interesting code below:
下面有一段有趣的代码:
def func1():
try:
return 1
finally:
return 2
def func2():
try:
raise ValueError()
except:
return 1
finally:
return 3
func1()
func2()
Could please somebody explain, what results will return these two functions and explain why, i.e. describe the order of the execution
可以请人解释一下,这两个函数返回什么结果并解释原因,即描述执行顺序
采纳答案by lejlot
From the Python documentation
来自 Python文档
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 is also executed “on the way out” when any other clause of the try statement is left via a break, continue or return statement. A more complicated example (having except and finally clauses in the same try statement works as of Python 2.5):
finally 子句总是在离开 try 语句之前执行,无论是否发生异常。当在 try 子句中发生异常并且没有被 except 子句处理(或者它已经发生在一个 except 或 else 子句中)时,它会在 finally 子句执行后重新引发。当 try 语句的任何其他子句通过 break、continue 或 return 语句离开时,finally 子句也会“在退出时”执行。一个更复杂的例子(在同一个 try 语句中的 except 和 finally 子句从 Python 2.5 开始工作):
So once the try/except block is left using return, which would set the return value to given - finally blocks will alwaysexecute, and should be used to free resources etc. while using there another return - overwrites the original one.
因此,一旦使用return留下 try/except 块,这会将返回值设置为给定 - finally 块将始终执行,并且应该用于释放资源等,同时使用另一个 return - 覆盖原始块。
In your particular case, func1()
return 2
and func2()
return 3
, as these are values returned in the finally blocks.
在您的特定情况下,func1()
return2
和func2()
return 3
,因为这些是 finally 块中返回的值。
回答by falsetru
func1()
returns 2. func2()
returns 3.
func1()
返回 2.func2()
返回 3。
finally
block is executed finally regardless or exception.
finally
无论异常还是异常,块最终都会执行。
You can see order of execution using debugger. For example, see a screencast.
您可以使用调试器查看执行顺序。例如,请参阅截屏视频。
回答by TerryA
Putting print
statements beforehand really, really helps:
把print
报表事先真的,确实有帮助:
def func1():
try:
print 'try statement in func1. after this return 1'
return 1
finally:
print 'after the try statement in func1, return 2'
return 2
def func2():
try:
print 'raise a value error'
raise ValueError()
except:
print 'an error has been raised! return 1!'
return 1
finally:
print 'okay after all that let\'s return 3'
return 3
print func1()
print func2()
This returns:
这将返回:
try statement in func1. after this return 1
after the try statement in func1, return 2
2
raise a value error
an error has been raised! return 1!
okay after all that let's return 3
3
You'll notice that python always returns the last thing to be returned, regardless that the code "reached" return 1
in both functions.
你会注意到 python 总是返回要返回的最后一个东西,不管代码return 1
在两个函数中“到达”了多少。
A finally
block is alwaysrun, so the last thing to be returned in the function is whatever is returned in the finally block. In func1
, that's 2. In func2
, that's 3.
一个finally
块始终运行,因此在函数返回的最后一件事情是无论是在finally块返回。在 中func1
,那是 2。在 中func2
,那是 3。
回答by 1408786user
It will always go to the finally
block, so it will ignore the return
in the try
and except
. If you would have a return
above the try
and except
, it would return that value.
它将总是去finally
块,所以它会忽略return
的try
和except
。如果你有一个return
在try
and之上except
,它会返回那个值。
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