return eats exception

后端 未结 4 2093
臣服心动
臣服心动 2020-11-28 10:44

I found the following behavior at least weird:

def errors():
    try:
        ErrorErrorError
    finally:
        return 10

print errors()
# print         


        
4条回答
  •  旧巷少年郎
    2020-11-28 11:20

    The exception disappears when you use return inside a finally clause. .. Is that documented anywhere?

    It is:

    If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception, it is re-raised at the end of the finally clause. If the finally clause raises another exception or executes a return or break statement, the saved exception is lost.

提交回复
热议问题