100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

finally Block in Python

How the finally clause guarantees cleanup code runs no matter what happens in a try block, including with return and break.

Exception HandlingBeginner9 min readJul 7, 2026
Analogies

1. Introduction

The finally clause attaches cleanup code to a try statement that is guaranteed to execute regardless of whether an exception occurred, whether it was caught, or even whether the try block exits early via return, break, or continue. It is the standard place to release resources such as closing files, releasing locks, or closing network connections.

🏏

Cricket analogy: Regardless of whether a match ends in a win, loss, or is abandoned for rain, the groundskeeper always rolls out the covers to protect the pitch afterward — cleanup that happens no matter how the match concluded.

finally runs after the try block and any matching except block have finished, and it runs even if no except clause is present at all.

🏏

Cricket analogy: The covers go on after the umpires call stumps, whether it was a normal close of play or an early finish due to bad light, and they still go on even if there was no rain delay at all during the day.

2. Syntax

python
try:
    risky_operation()
except SomeError as e:
    handle(e)
finally:
    cleanup()   # always runs, exception or not

3. Explanation

finally runs in every case: when the try block completes normally, when an exception is raised and caught by an except clause, when an exception is raised and not caught at all (finally still runs before the exception propagates further), and even when the try block hits a return, break, or continue statement.

🏏

Cricket analogy: The pitch gets covered whether the match finishes its full course, a batsman is controversially given out and the umpire's decision holds, the decision is overturned entirely, or the umpires abandon play early due to bad light — covers go on every single time.

finally always runs even after a return statement inside try. If try has return 1 and finally has return 2, the finally's return value overrides the try's return value — this is a well-known gotcha. In general, avoid returning from within a finally block, since it silently discards whatever the try/except was about to return or any exception that was about to propagate.

Because of this guarantee, finally is ideal for cleanup that must happen unconditionally, such as file.close() or lock.release(), though the with statement (context managers) is usually preferred for resource cleanup because it expresses the same guarantee more concisely.

🏏

Cricket analogy: Rolling the covers onto the pitch after every session is non-negotiable groundskeeping, similar to file.close(), though many modern grounds now use automated covering systems that guarantee the same protection more reliably than manual crews.

4. Example

python
def demo_normal():
    try:
        print("try: running normally")
    finally:
        print("finally: cleanup after normal run")

def demo_exception():
    try:
        raise ValueError("boom")
    except ValueError as e:
        print(f"except: caught {e}")
    finally:
        print("finally: cleanup after exception")

def demo_return_override():
    try:
        return 1
    finally:
        print("finally: runs even after return")
        return 2

demo_normal()
demo_exception()
print("return value:", demo_return_override())

5. Output

text
try: running normally
finally: cleanup after normal run
except: caught boom
finally: cleanup after exception
finally: runs even after return
return value: 2

6. Key Takeaways

  • finally always executes, whether or not an exception was raised.
  • finally runs even if the exception is uncaught, right before it propagates.
  • finally runs even when try exits via return, break, or continue.
  • A return statement inside finally overrides any return from try or except.
  • finally is the standard place for guaranteed cleanup like closing files or releasing locks.
  • The with statement is often a cleaner alternative for resource cleanup.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#FinallyBlockInPython#Finally#Block#Syntax#Explanation#StudyNotes#SkillVeris