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.
Discussions

Is there any point to the finally keyword?
The rule is enforced, that's what makes it useful. Even if you fail to handle an exception, finally runs regardless - unless you trigger a stack overflow in which case the interpreter crashes. try: raise ValueError finally: print("Now you see me") Without finally, try: raise ValueError except TypeError: pass print("Now you don't") you wouldn't. You can also do something quite funky with it. def test(): try: return 69 finally: return 1337 print(test()) # What do you think is going to happen? More on reddit.com
🌐 r/learnpython
12
7
April 12, 2024
Jump statement in try except finally block
Hi all, can someone say why this is stuck in a infinite loop? Code while True: print("why is this happening") try: break finally: continue Output why is this happening why is this happening why is this happening ... More on discuss.python.org
🌐 discuss.python.org
2
June 29, 2024
Run code finally before returning in except
try: # Some Code.... except: # Handling of exception return False else: # execute if no exception finally: print('done') # Execute before returning False incase of an exception How do I get the code in finally to execute before returning False in except ? More on discuss.python.org
🌐 discuss.python.org
0
December 6, 2023
What's the point of "finally" in "try except" block?
finally will always run, no matter what the result of try or except is, even if one of them contains break or return def tell_me_about_bad_math(): try: 1/0 except ZeroDivisionError: return 'oops!' finally: print('you did bad math!') print(tell_me_about_bad_math()) More on reddit.com
🌐 r/learnpython
14
17
September 22, 2020
🌐
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 ...
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › python › python_tryfinally_block.htm
Python - The try-finally Block
try: # Code that might raise exceptions risky_code() finally: # Code that always runs, regardless of exceptions cleanup_code() 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.
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.2 documentation
Traceback (most recent call last): File "<stdin>", line 2, in <module> raise KeyboardInterrupt KeyboardInterrupt · If a finally clause is present, the finally clause will execute as the last task before the try statement completes.
Find elsewhere
🌐
Quora
quora.com › How-does-the-finally-clause-work-in-Pythons-try-except-blocks
How does the finally clause work in Python's try-except blocks? - Quora
Answer: It's a default block of code that executes irrespective of exception. It's generally put after the catch block. It only have some default statements to be executed.
🌐
Medium
medium.com › @kawsarlog › understanding-the-power-of-the-finally-block-in-python-programming-bccdd288e470
Understanding the Power of the ‘finally’ Block in Python Programming | by MD Kawsar | Medium
May 24, 2024 - Without finally, critical cleanup code may not execute if an unexpected exception occurs or if the block is exited via control statements like return. Failing to execute cleanup operations can lead to resource leaks and unstable applications, which are often difficult and costly to debug. Even attempts to exit the Python interpreter using sys.exit() will not prevent the finally clause from executing, demonstrating its robustness.
🌐
Tutorialspoint
tutorialspoint.com › python › python_finally_keyword.htm
Python finally Keyword
Following is the syntax of the Python finally keyword − ... var1 = 2 var2 = 'nine' try: print(var1//var2) except Exception as e: print("Error :",e) finally: print("Hello, Welcome to Tutorialspoints") ... The finally can be used without except block. The finally block is executed even there is an error raised in the try block.
🌐
Board Infinity
boardinfinity.com › blog › try-except-in-python
Try, Except, Else and Finally in Python | Board Infinity
August 14, 2025 - In Python, we use the final keyword, which is executed always even if the exception is not handled and the try-except block is terminated.
🌐
YouTube
youtube.com › watch
Python Exceptions - Exception Handling using the Finally Block - Try Except Finally Code Example - YouTube
After the except blocks, you can have one finally block, which is used to clean-up code actions such as:- Close a connection to a database or file- Printing ...
Published   November 11, 2021
🌐
W3Schools
w3schools.com › python › gloss_python_try_finally.asp
Python Try Finally
Python Overview Python Built-in ... Python Certificate Python Training ... The finally block, if specified, will be executed regardless if the try block raises an error or not....
🌐
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
In Python's exception handling sequence, the finally block follows the try and except blocks. It begins with the finally keyword and a colon, which is followed by indented lines of code that get executed unconditionally:
🌐
Python.org
discuss.python.org › python help
Jump statement in try except finally block - Python Help - Discussions on Python.org
June 29, 2024 - Hi all, can someone say why this is stuck in a infinite loop? Code while True: print("why is this happening") try: break finally: continue Output why is this happening why is this happening why is this happening ...
🌐
DataCamp
datacamp.com › tutorial › exception-handling-python
Exception & Error Handling in Python | Tutorial by DataCamp | DataCamp
December 12, 2024 - The finally keyword in the try-except block is always executed, irrespective of whether there is an exception or not. In simple words, the finally block of code is run after the try, except and else block is final.
🌐
Python.org
discuss.python.org › python help
Run code finally before returning in except - Python Help - Discussions on Python.org
December 6, 2023 - try: # Some Code.... except: # Handling of exception return False else: # execute if no exception finally: print('done') # Execute before returning False incase of an exce…
🌐
GeeksforGeeks
geeksforgeeks.org › python › try-except-else-and-finally-in-python
Try, Except, else and Finally in Python - GeeksforGeeks
Python provides a keyword finally, which is always executed after try and except blocks. The finally block always executes after normal termination of try block or after try block terminates due to some exception.
Published   July 15, 2025
🌐
GUVI
guvi.in › hub › python › try-except-finally-in-python
try…except…finally in Python
In Python, the 'try...except...finally' statement is an extended version of the 'try...except' statement that includes a 'finally' block. The 'finally' block is executed regardless of whether an exception occurred or not.
🌐
Studytonight
studytonight.com › python › finally-block-in-python
Python finally block | Studytonight
The finally block is always executed, so it is generally used for doing the concluding tasks like closing file resources or closing database connection or may be ending the program execution with a delightful message. If in your code, the except block is unable to catch the exception and the ...