爲什麼foo
這個異常不被注意到,但bar
的異常被引發?python try-finally
def foo():
try:
raise Exception('foo')
finally:
return
def bar():
try:
raise Exception('bar')
finally:
pass
foo()
bar()
爲什麼foo
這個異常不被注意到,但bar
的異常被引發?python try-finally
def foo():
try:
raise Exception('foo')
finally:
return
def bar():
try:
raise Exception('bar')
finally:
pass
foo()
bar()
如果最後條款引發了另一個異常或執行了return或break語句,保存異常丟失。
重複的[return eats exception](http://stackoverflow.com/questions/517060/return-eats-exception) – gecco 2011-12-20 12:04:51