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 aTypeError. - Other control flow statements such as
continueandbreakstatements.
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.
Videos
06:30
What's So Special About Finally in Python? - YouTube
Is "finally" Useless In Python?
02:23
Python Finally Statement - YouTube
08:05
Finally keyword in Python | Python Tutorial - Day #37 - YouTube
27:11
#21 Exception Handling In Python- Try, Except,Finally,Raise Keyword- ...
02:00
Python Exceptions - Exception Handling using the Finally Block ...
Top answer 1 of 16
762
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 aTypeError. - Other control flow statements such as
continueandbreakstatements.
2 of 16
153
You can use finally to make sure files or resources are closed or released regardless of whether an exception occurs, even if you don't catch the exception. (Or if you don't catch that specific exception.)
myfile = open("test.txt", "w")
try:
myfile.write("the Answer is: ")
myfile.write(42) # raises TypeError, which will be propagated to caller
finally:
myfile.close() # will be executed before TypeError is propagated
In this example you'd be better off using the with statement, but this kind of structure can be used for other kinds of resources.
A few years later, I wrote a blog post about an abuse of finally that readers may find amusing.
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.')...
Call +917738666252
Address Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
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.
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.
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 ·