It makes a difference if you return early:

try:
    run_code1()
except TypeError:
    run_code2()
    return None   # The finally block is run before the method returns
finally:
    other_code()

Compare to this:

try:
    run_code1()
except TypeError:
    run_code2()
    return None   

other_code()  # This doesn't get run if there's an exception.

Other situations that can cause differences:

  • If an exception is thrown inside the except block.
  • If an exception is thrown in run_code1() but it's not a TypeError.
  • Other control flow statements such as continue and break statements.
Answer from Mark Byers on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_keyword_finally.asp
Python finally Keyword
Python Examples Python Compiler Python Exercises Python Quiz Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... try: x > 3 except: print("Something went wrong") else: print("Nothing went wrong") finally: print("The try...except block is finished") Try it Yourself » · The finally keyword is used in try...except blocks.
🌐
Real Python
realpython.com › ref › keywords › finally
finally | Python Keywords – Real Python
In Python, the finally keyword is used in a try statement to define a block of code that will always execute, regardless of whether an exception was raised. This keyword is especially useful for cleaning up resources or performing any necessary ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › finally-keyword-in-python
Finally keyword in Python - GeeksforGeeks
July 11, 2025 - In Python, the finally keyword is used in a try-except-finally block to define a section of code that will always execute, regardless of whether an exception occurs or not. It guarantees predictable code behavior, maintaining program stability even when errors arise.
🌐
GeeksforGeeks
geeksforgeeks.org › python › try-except-else-and-finally-in-python
Try, Except, else and Finally in Python - GeeksforGeeks
Example: Let's try to throw the exception in except block and Finally will execute either exception will generate or not ... # Python code to illustrate # working of try() def divide(x, y): try: # Floor Division : Gives only Fractional # Part ...
Published   July 15, 2025
🌐
Medium
medium.com › pythoneers › python-finally-keyword-complete-guide-with-examples-2025-update-e598c3c376e0
Python Finally Keyword: Complete Guide with Examples (2025 Update) | by Naveen Pandey | The Pythoneers | Medium
June 23, 2025 - In this comprehensive guide, we’ll explore the power of the finally keyword, understand its practical applications, and learn about the upcoming changes in Python 3.14. The finally block executes code no matter what happens in your try-except structure. Regardless of what you write in your try or except blocks, finally will always be executed, which makes it great for cleanup operations. Let’s start with a practical example that demonstrates the power of finally.
🌐
TutorialsPoint
tutorialspoint.com › finally-keyword-in-python
Finally keyword in Python
try: # main python Code.... except: # It is optional block # Code to handle exception finally: # This Code that is always executed
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › finally in python
Finally in Python | Functions of Finally Keyword in Python
April 12, 2023 - C# program to demonstrate the use ... finally: print('This is finally block of code which is always executed regardless of the exception is raised or not or if it is raised, regardless of it is handled or not.')...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
🌐
Codecademy
codecademy.com › docs › python › keywords › finally
Python | Keywords | finally | Codecademy
July 2, 2025 - ... try: # Code that may raise an exception ... except SomeException: # Code to handle the exception ... finally: # Code that will run no matter what ... ... Running try block... This will always run.
🌐
Codingem
codingem.com › home › python “finally” statement: an ultimate guide (with examples)
Python "finally" Statement: An Ultimate Guide (with Examples)
November 5, 2022 - There was an error Yay Traceback (most recent call last): File "example.py", line 3, in with_finally print(x) NameError: name 'x' is not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "example.py", line 10, in <module> with_finally() File "example.py", line 6, in with_finally print(y) NameError: name 'y' is not defined · Today you learned what is the point of the finally statement in Python error handling.
🌐
Tutorialspoint
tutorialspoint.com › python › python_finally_keyword.htm
Python finally Keyword
The Python, finally keyword is used along with try and except block. This block is executed irrespective of the error raised in the try block. It can used without the except block, thought the try block raise an error, the finally can be executed.
🌐
Pythontic
pythontic.com › concepts › keywords › finally
finally keyword in Python | Pythontic.com
Freeing of such resources by calling the relevant close() methods can be done inside the finally block. In the Python example below the file open mode is by mistake given as “wb” which triggers an exception after the file has been opened and a write operation is invoked.
🌐
Embedded Inventor
embeddedinventor.com › home › python finally keyword explained with examples
Python finally keyword explained with examples
December 27, 2023 - Also, you can just execute the finally block after the try block without using the except block. ... try: print(1/0) except ZeroDivisionError: print("Error: You cannot divide by 0. Please try again.") finally: print("This is always executed!")
🌐
CodeSignal
codesignal.com › learn › courses › debugging-code-using-python › lessons › mastering-the-finally-block-in-pythons-exception-handling
Mastering the 'Finally' Block in Python's Exception Handling
Consider a program that attempts to divide 10 by 0, thereby triggering a ZeroDivisionError. Observe how the finally block executes, despite the exception:
🌐
GUVI
guvi.in › hub › python › try-except-finally-in-python
try…except…finally in Python
This example demonstrates how 'try...except...finally' allows you to handle exceptions and execute critical code in the 'finally' block, ensuring that necessary cleanup or resource management tasks are always performed, even in the presence of exceptions. To conclude, the try...except statement in Python provides a robust and comprehensive approach to exception handling and resource management.
🌐
Python documentation
docs.python.org › 3 › reference › compound_stmts.html
8. Compound statements — Python 3.14.2 documentation
If the finally clause raises another ... break or continue statement, the saved exception is discarded. For example, this function returns 42....
🌐
Dydevops
dydevops.com › tutorials › python › python-finally-keyword
Python finally Keyword – Explained with Examples - Python Tutorial | DyDevOps
May 20, 2025 - Explanation: Even though no except ... is thrown. try: file = open("example.txt", "r") content = file.read() except FileNotFoundError: print("File not found") finally: file.close() print("File closed")...
🌐
Tutorialspoint
tutorialspoint.com › python › python_tryfinally_block.htm
Python - The try-finally Block
In Python, when using exception handling with try blocks, you have the option to include either except clauses to catch specific exceptions or a finally clause to ensure certain cleanup operations are executed, but not both together. Let us consider an example where we want to open a file in write mode ("w"), writes some content to it, and ensures the file is closed regardless of success or failure using a finally block −
🌐
PythonForBeginners.com
pythonforbeginners.com › home › python finally keyword and uses
Python Finally Keyword and Uses - PythonForBeginners.com
May 2, 2023 - We use the finally block to mandatorily execute a code in Python. The syntax for using the finally block is as follows. try: #do something except: #do something once error occurs finally: #you must do it. In the above syntax, the statements in the try block are executed normally.
🌐
Studytonight
studytonight.com › python › finally-block-in-python
Python finally block | Studytonight
Access Modifers in Python · Types of Inheritance · Method Overriding · Polymorphism · static Keyword · Operator Overloading · Introduction to Error Handling · Exception Handling: try and except · Exeption Handling: finally · Exception Handling: raise ·