4
python 在finally中放return导致except中的raise没有抛出的坑
source link: https://qidizi.github.io/python-finally%E4%B8%AD%E6%94%BEreturn%E5%AF%BC%E8%87%B4except%E7%9A%84raise%E6%B6%88%E5%A4%B1/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
python 在finally中放return导致except中的raise没有抛出的坑
def e_try():
try:
raise Exception('我的错')
except Exception as e:
print('捉到:%s' % e)
raise
finally:
print('来到finally')
return print('return了,你见不到except中raise的东西罗') # TODO 这是不小心的坑
e_try()
def e_try2():
try:
raise Exception('我的错')
except Exception as e:
print('捉到:%s' % e)
raise
finally:
print('来到finally,在我之后,你会见到except中raise信息,因为它在我之后处理')
return print('raise暴发了,你看不到我的')
e_try2()
运行结果:
mac:edm-python qidizi$ python3 test.py
捉到:我的错
来到finally
return了,你见不到except中raise的东西罗
捉到:我的错
来到finally,在我之后,你会见到except中raise信息,因为它在我之后处理
Traceback (most recent call last):
File "test.py", line 27, in <module>
e_try2()
File "test.py", line 17, in e_try2
raise Exception('我的错')
Exception: 我的错
mac:edm-python qidizi$
原因是finally先执行,再去处理except中的raise
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK