Language Syntax

Finally — Python syntax

Execute cleanup when leaving the associated try, including on exceptions and returns. Returning or raising a new error inside finally can replace pending control flow; avoid masking errors accidentally.

Formal Syntax & Structure

python syntaxpython
finally:    suite

Working Example

Example Snippetpython
seen=[]def f():    try:        return 7    finally:        seen.append('cleanup')print((f(),seen))
Python Reference