python try-finally

别等时光非礼了梦想. 提交于 2019-11-28 02:48:50

问题


Why does the exception in foo whizz by unnoticed, but the exception in bar is raised?

def foo():
    try:
        raise Exception('foo')
    finally:
        return

def bar():
    try:
        raise Exception('bar')
    finally:
        pass

foo()
bar()

回答1:


From the Python documentation:

If the finally clause raises another exception or executes a return or break statement, the saved exception is lost.



来源:https://stackoverflow.com/questions/8574856/python-try-finally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!