Home Course Roadmap
Home / Python Course / Lesson 41
Lesson 41 Error Handling

Python finally

Learn how Python's finally block guarantees that important cleanup code runs whether an operation succeeds or raises an exception.

35–45 minutes Beginner Error Handling
0%
01

Learning Objectives

By the end of this lesson, you will understand how the finally block works and when it should be used in real Python programs.

You will learn how to:

  • Understand the purpose of finally.
  • Use try / except / finally.
  • Understand when finally executes.
  • Combine else and finally.
  • Perform cleanup operations safely.
  • Use finally with files and resources.
  • Understand finally with return.
  • Avoid common mistakes.
02

What Is finally?

The finally block is part of Python's exception-handling system.

Its most important feature is that it normally runs whether an exception occurs or not.

A basic structure looks like this:

Python
try:
    # Code that might fail

except SomeException:
    # Handle the exception

finally:
    # Code that should run afterward
Core idea

The finally block is commonly used for cleanup work that should happen regardless of whether the operation succeeded or failed.

03

Why Use finally?

Programs often acquire resources temporarily. Examples include:

  • Opening a file
  • Connecting to a database
  • Opening a network connection
  • Acquiring a lock
  • Starting a temporary operation
  • Creating temporary resources

Once the operation is finished, these resources may need to be released.

Think of finally as cleanup time.

Even if something goes wrong, your program gets an opportunity to perform necessary cleanup.

04

Basic finally Syntax

The simplest form uses try followed by finally.

Python
try:
    print("Starting operation")

finally:
    print("Cleanup operation")
Output
Starting operation
Cleanup operation

Notice that there is no except block here. Python allows try to be followed directly by finally.

05

When No Exception Occurs

If the code inside try succeeds, Python still executes finally.

Python
try:
    number = 10
    print("Number:", number)

finally:
    print("Operation finished.")
Output
Number: 10
Operation finished.

The operation succeeded, but the cleanup section still executed.

06

When an Error Occurs

The finally block also runs when an exception occurs.

Python
try:
    number = 10 / 0

finally:
    print("Cleanup executed.")
Output Before Traceback
Cleanup executed.

After the finally block executes, the unhandled ZeroDivisionError can continue upward and produce a traceback.

Important distinction

finally does not automatically handle an exception. It executes cleanup; an except block is what handles the exception.

07

try / except / finally

The most common complete structure combines all three blocks.

Python
try:
    number = int("abc")
    print(number)

except ValueError:
    print("Invalid number.")

finally:
    print("Program section finished.")
Output
Invalid number.
Program section finished.
The flow
  1. Python enters try.
  2. An exception occurs.
  3. Matching except handles it.
  4. finally executes.
  5. The program continues after the structure.
08

else + finally

Python allows all three optional components to work together:

Python
try:
    number = int("25")

except ValueError:
    print("Invalid input.")

else:
    print("Conversion successful.")

finally:
    print("Finished.")
Output
Conversion successful.
Finished.

The three blocks have different responsibilities:

  • try: attempt the operation.
  • except: handle an exception.
  • else: run after successful execution.
  • finally: perform final cleanup.
09

Cleanup Operations

Cleanup means returning your program or environment to an appropriate state after an operation.

Examples include:

  • Closing a resource
  • Releasing a lock
  • Closing a connection
  • Deleting temporary resources
  • Writing final log information
  • Resetting temporary state
Python
try:
    print("Processing data...")

except Exception as error:
    print("Something went wrong:", error)

finally:
    print("Cleaning up resources...")

The cleanup message is intended to represent work that should happen after the main operation.

10

Using finally with Files

Before Python's with statement became the preferred way to manage files, developers commonly used finally to make sure files were closed.

Python
file = None

try:
    file = open("students.txt", "r", encoding="utf-8")
    data = file.read()
    print(data)

except FileNotFoundError:
    print("File not found.")

finally:
    if file is not None:
        file.close()

    print("File operation finished.")
Modern Python approach

For ordinary file handling, prefer with open(...). Python's context manager automatically handles closing the file.

Preferred Approach
with open("students.txt", "r", encoding="utf-8") as file:
    data = file.read()

print(data)

Understanding finally is still important because the same cleanup principle applies to many resources beyond simple file reading.

11

Resource Management

A resource is something your program temporarily acquires and should eventually release.

Examples include:

  • Database connections
  • Network connections
  • Files
  • Locks
  • Temporary resources

Failing to release resources can cause problems such as resource leaks, locked files, connection exhaustion or unexpected application behavior.

Resource lifecycle

Acquire → Use → Release

12

finally and return

The interaction between finally and return is important.

The finally block executes before a function actually returns.

Python
def example():
    try:
        return "Returning from try"

    finally:
        print("Finally runs first.")

print(example())
Output
Finally runs first.
Returning from try
Avoid returning from finally.

A return inside finally can override a return value or even suppress an exception. This usually makes code confusing and difficult to debug.

13

Nested finally

Exception-handling structures can be nested when complex resource management requires it.

Python
try:
    print("Outer try")

    try:
        print("Inner try")

    finally:
        print("Inner cleanup")

finally:
    print("Outer cleanup")
Output
Outer try
Inner try
Inner cleanup
Outer cleanup

The inner finally executes when the inner operation finishes, followed by the outer cleanup.

Keep nesting under control.

If your exception-handling structure becomes deeply nested, consider reorganizing the code into smaller functions.

14

Real-World Examples

Example 1 — Database Connection

A database application may need to close a connection after an operation.

Conceptual Example
connection = None

try:
    connection = connect_to_database()
    perform_query(connection)

except DatabaseError:
    print("Database operation failed.")

finally:
    if connection is not None:
        connection.close()

Example 2 — Processing a Report

Python
try:
    print("Generating report...")
    generate_report()

except Exception as error:
    print("Report failed:", error)

finally:
    print("Report process ended.")

Example 3 — Releasing a Lock

Python
lock.acquire()

try:
    update_shared_data()

finally:
    lock.release()
The important pattern

Acquire a resource, perform the operation, and make sure the resource is released.

15

Common Mistakes

1. Thinking finally Handles Exceptions

It does not. finally performs final cleanup. An except block handles a matching exception.

2. Returning from finally

Avoid this because it can override previous return values and suppress exceptions.

3. Using finally Everywhere

Use it when there is meaningful finalization or cleanup work to perform.

4. Ignoring Better Resource Management Tools

For files and many other resources, Python's with statement is usually clearer and safer.

5. Putting Unrelated Code in finally

Keep the cleanup block focused on operations that should happen after the main operation.

Remember

finally is about guaranteed finalization—not about hiding errors.

16

Practice Exercises

Try solving these exercises without looking at the examples above.

Exercise 1 — Basic finally

Create a try / finally program that prints a message from both blocks.

Exercise 2 — Division

Create a division program that uses except to handle zero and finally to print a completion message.

Exercise 3 — Input Validation

Ask the user for an integer and use try / except / finally.

Exercise 4 — File Cleanup

Open a file and use finally to ensure the file is closed.

Exercise 5 — else + finally

Create a program that uses all four concepts: try, except, else and finally.

Exercise 6 — Return

Create a function with a return inside try and observe when finally executes.

Exercise 7 — Resource Cleanup

Design a small program that acquires a resource, uses it, and releases it inside finally.

17

Mini Challenge — Safe File Processor

Build a small program that processes a text file while demonstrating proper exception handling and cleanup.

Your program should:
  • Try to open a file.
  • Read its contents.
  • Handle FileNotFoundError.
  • Display the contents when successful.
  • Use finally for cleanup.
  • Print a final completion message.

Starter Structure

Python
file = None

try:
    file = open("students.txt", "r", encoding="utf-8")

    data = file.read()

    print("File contents:")
    print(data)

except FileNotFoundError:
    print("The file could not be found.")

finally:
    if file is not None:
        file.close()

    print("File processing finished.")
Challenge extension

Modify the program so it counts the number of lines and words in the file. Then display a small report after successful processing.

18

Quick Knowledge Check

Test your understanding of Python's finally block.

1. What is the main purpose of finally?

2. Does finally normally run when no exception occurs?

3. Does finally handle an exception by itself?

4. Which structure can contain try, except, else and finally?

5. What is a common use of finally?

6. Which statement is generally discouraged?

7. Which Python feature is usually preferred for managing files?

8. What does else do in try / except / else / finally?

9. Can try be followed directly by finally?

10. What is the key idea behind finally?

19

Lesson Summary

In this lesson, you learned how the finally block works and why it is useful for cleanup and resource management.

finally is used for finalization and cleanup.
It normally executes whether an exception occurs or not.
finally does not itself handle exceptions.
try / except / finally is useful when an operation needs both error handling and cleanup.
else can be used for code that should run only after successful execution.
finally is useful for releasing resources such as connections and locks.
Avoid returning from finally because it can suppress earlier results or exceptions.
For ordinary file management, prefer the with statement.
Key takeaway

finally is about making sure important finalization work gets a chance to happen. It is especially valuable when your program works with resources that must be released safely.

You've reached the end!

You now understand how finally works and how it fits together with try, except and else.