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
🌐
GeeksforGeeks
geeksforgeeks.org › python › try-except-else-and-finally-in-python
Try, Except, else and Finally in Python - GeeksforGeeks
Example: In this code, The system can not divide the number with zero so an exception is raised. ... Traceback (most recent call last): File "/home/8a10be6ca075391a8b174e0987a3e7f5.py", line 3, in <module> print(a/b) ZeroDivisionError: division ...
Published   July 15, 2025
🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
In this example, the try block does not generate any error: try: print("Hello") except: print("Something went wrong") else: print("Nothing went wrong") Try it Yourself » · The finally block, if specified, will be executed regardless if the ...
Discussions

exception - Why do we need the “finally:” clause in Python, if we can just write code after the “try:” block? - Stack Overflow
This would've worked even if you ... difference, a good example would've been causing a different error than IOError, to show that the finally clause block is executed before the exception is propagated to the caller. 2019-03-07T15:13:29.567Z+00:00 ... ... try: a/b print('In try ... More on stackoverflow.com
🌐 stackoverflow.com
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
18
September 22, 2020
exception - What is the intended use of the optional "else" clause of the "try" statement in Python? - Stack Overflow
So, if you have a method that could, for example, throw an IOError, and you want to catch exceptions it raises, but there's something else you want to do if the first operation succeeds, and you don't want to catch an IOError from that operation, you might write something like this: try: operation_that_can_throw_ioerror() except IOError: handle_the_exception_somehow() else: # we don't want to catch the IOError if it's raised another_operation_that_can_throw_ioerror() finally... More on stackoverflow.com
🌐 stackoverflow.com
What happens when you use try and except statements in a function that both have a return statement in them BUT you also have a finally statement?
It doesn't ignore anything, It's just that the finally block is guaranteed to always be called, so it's triggered at the point the return statement is reached. The docs cover this explicitly: When a return, break or continue statement is executed in the try suite of a try…finally statement, the finally clause is also executed ‘on the way out.’ More on reddit.com
🌐 r/learnpython
9
9
May 30, 2023
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.3 documentation
The try statement has another optional clause which is intended to define clean-up actions that must be executed under all circumstances. For example: >>> try: ... raise KeyboardInterrupt ... finally: ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › statements › exception-handling-statements
Exception-handling statements - throw and try, catch, finally - C# reference | Microsoft Learn
January 20, 2026 - The following example uses a throw ... execution of the code inside a try block, try-finally - to specify the code that runs when control leaves the try block, and try-catch-finally - as a combination of the preceding two ...
🌐
Programiz
programiz.com › python-programming › exception-handling
Python Exception Handling (With Examples)
April 11, 2024 - try: numerator = 10 denominator = 0 result = numerator/denominator print(result) except: print("Error: Denominator cannot be 0.") finally: print("This is finally block.") ... Error: Denominator cannot be 0. This is finally block. In the above example, we are dividing a number by 0 inside the ...
Find elsewhere
🌐
Python Tutorial
pythontutorial.net › home › python basics › python try…except…finally
Python try...except...finally Statement - Python Tutorial
1 month ago - a = 10 b = 0 try: c = a / b print(c) except ZeroDivisionError as error: print(error) finally: print('Finishing up.') Code language: PHP (php) ... In this example, the try clause causes a ZeroDivisionError exception both except and finally clause ...
🌐
Medium
galea.medium.com › pythons-try-except-else-finally-explained-f04d47d57125
Python’s “try except else finally” explained | by Alex Galea | Medium
October 4, 2020 - Understanding else/finally is simple because there are only two cases. The try statement succeeds -> The except statement is skipped -> The else statement runs -> The finally statement runs
🌐
w3resource
w3resource.com › python › python-try-except-with-examples.php
Python Try-Except with Examples: Else, Finally, and More
The 'try' block attempts to convert user input to an integer and divide it. The 'except' blocks handle potential errors. The 'else' block runs only if no exceptions occur. The 'finally' block runs at the end, no matter what.
🌐
Python
docs.python.org › 2.5 › whatsnew › pep-341.html
6 PEP 341: Unified try/except/finally
July 31, 2012 - Guido van Rossum spent some time working with Java, which does support the equivalent of combining except blocks and a finally block, and this clarified what the statement should mean. In Python 2.5, you can now write: try: block-1 ... except Exception1: handler-1 ...
Top answer
1 of 16
1150

The statements in the else block are executed if execution falls off the bottom of the try - if there was no exception. Honestly, I've never found a need.

However, Handling Exceptions notes:

The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try ... except statement.

So, if you have a method that could, for example, throw an IOError, and you want to catch exceptions it raises, but there's something else you want to do if the first operation succeeds, and you don't want to catch an IOError from that operation, you might write something like this:

try:
    operation_that_can_throw_ioerror()
except IOError:
    handle_the_exception_somehow()
else:
    # we don't want to catch the IOError if it's raised
    another_operation_that_can_throw_ioerror()
finally:
    something_we_always_need_to_do()

If you just put another_operation_that_can_throw_ioerror() after operation_that_can_throw_ioerror, the except would catch the second call's errors. And if you put it after the whole try block, it'll always be run, and not until after the finally. The else lets you make sure

  1. the second operation's only run if there's no exception,
  2. it's run before the finally block, and
  3. any IOErrors it raises aren't caught here
2 of 16
172

There is one big reason to use else - style and readability. It's generally a good idea to keep code that can cause exceptions near the code that deals with them. For example, compare these:

try:
    from EasyDialogs import AskPassword
    # 20 other lines
    getpass = AskPassword
except ImportError:
    getpass = default_getpass

and

try:
    from EasyDialogs import AskPassword
except ImportError:
    getpass = default_getpass
else:
    # 20 other lines
    getpass = AskPassword

The second one is good when the except can't return early, or re-throw the exception. If possible, I would have written:

try:
    from EasyDialogs import AskPassword
except ImportError:
    getpass = default_getpass
    return False  # or throw Exception('something more descriptive')

# 20 other lines
getpass = AskPassword

Note: Answer copied from recently-posted duplicate here, hence all this "AskPassword" stuff.

🌐
Note.nkmk.me
note.nkmk.me › home › python
Try, except, else, finally in Python (Exception handling) | note.nkmk.me
July 31, 2023 - If you want to catch an exception and continue without taking any action, use pass. def divide_pass(a, b): try: print(a / b) except ZeroDivisionError: pass divide_pass(1, 0) ... See the following article for details on the pass statement.
🌐
GUVI
guvi.in › hub › python › try-except-finally-in-python
try…except…finally in Python
In the first call to 'divide_numbers(10, 2)', the division is successful, and the 'except' block is not triggered. The program then proceeds to the 'finally' block, which is always executed.
🌐
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
There’s 4 parts - try (and do something useful that may fail), except (something has failed and we need to do something different), else (all is fine, no errors were caught, so time to finish up) and finally (we do this before we exit).
🌐
Python for Network Engineers
pyneng.readthedocs.io › en › latest › book › 06_control_structures › exceptions.html
Working with try/except/else/finally - Python for network engineers
ZeroDivisionError exception raised ... # -*- coding: utf-8 -*- try: a = input("Enter first number: ") b = input("Enter second number: ") print("Result: ", int(a)/int(b)) except (ValueError, ZeroDivisionError): print("Something went wrong...")...
🌐
W3Schools
w3schools.com › python › ref_keyword_finally.asp
Python finally Keyword
Python Examples Python Compiler ... 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 »...
🌐
freeCodeCamp
freecodecamp.org › news › error-handling-in-python-introduction
Error Handling in Python – try, except, else, & finally Explained with Code Examples
January 13, 2025 - Back to my first example, I changed our variable x back to the value 0 and tried to divide 5 by x. This produces a ZeroDivisionError. Since my except statement specifies this type of exception, the code in that clause executes before the program resumes running as normal. Finally, if the program ...
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
December 1, 2024 - That file didn’t exist, but instead of letting the program crash, you caught the FileNotFoundError exception and printed a message to the console. ... Imagine that you always had to implement some sort of action to clean up after executing your code. Python enables you to do so using the finally clause: ... # ... try: linux_interaction() except RuntimeError as error: print(error) else: try: with open("file.log") as file: read_data = file.read() except FileNotFoundError as fnf_error: print(fnf_error) finally: print("Cleaning up, irrespective of any exceptions.")
🌐
Inventive HQ
inventivehq.com › home › blog › automation › python try except: complete error handling guide
Python Try Except: Complete Error Handling Guide
November 6, 2025 - Catch them separately if you need different handling for each error type. Group them together if the handling is the same. Example: except (FileNotFoundError, PermissionError) as e: if both errors should log and skip the file.