Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exit and finally

  • finally

The "finally" part will be called even if we call "return" or "exit" in the "try" block.


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

def g():
    try:
        exit()
    finally:
       print("finally in g")

print("before")
f()
print("after f")
g()
print("after g")

# before
# finally in f
# after f
# finally in g