[Short text] When returning in both try clause, except clause and finally clause

Suddenly I was curious, so I tried it. Confirmed with Python 2.7 and Python 3.4.

return in try and finally clauses


def f():
    try:
        return "try"
    finally:
        return "finally"

print(f())

finally

return in except and finally clauses


def f():
    try:
        raise ValueError
    except ValueError:
        return "except"
    finally:
        return "finally"

print(f())

finally

I probably won't use it. I did not know.

Recommended Posts

[Short text] When returning in both try clause, except clause and finally clause
When you run diff in python and want both returncode and output
EP 13 Take Advantage of Each Block in try / except / else / finally
Reading and writing text in Python