From the Python documentation

A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in a except or else clause), it is re-raised after the finally clause has been executed. The finally clause is also executed “on the way out” when any other clause of the try statement is left via a break, continue or return statement. A more complicated example (having except and finally clauses in the same try statement works as of Python 2.5):

So once the try/except block is left using return, which would set the return value to given - finally blocks will always execute, and should be used to free resources etc. while using there another return - overwrites the original one.

In your particular case, func1() returns 2 and func2() returns 3, as these are values returned in the finally blocks.

Answer from lejlot on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › 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?
r/learnpython on Reddit: 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?
May 30, 2023 -

Ok, that was an awful title and I'm sorry... but it's hard to phrase! My question overall is, if we use a try statement in a function (and let's imagine it works, we don't end up having to handle any exceptions) and there is a return statement in this try-block. Won't that cause us to leave the function? Since, we are returning control back to main, let's say.

But, we have a finally statement in our function to. It might do something trivial like print something. Does this get executed even though we should have hit return?

Now, I have tested this. And what it seems to do is reach the return statement, ignore it, carry out the finally statement and then go back to the return. But I would like to know if I am understanding this correctly.

Top answer
1 of 4
242

From the Python documentation

A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in a except or else clause), it is re-raised after the finally clause has been executed. The finally clause is also executed “on the way out” when any other clause of the try statement is left via a break, continue or return statement. A more complicated example (having except and finally clauses in the same try statement works as of Python 2.5):

So once the try/except block is left using return, which would set the return value to given - finally blocks will always execute, and should be used to free resources etc. while using there another return - overwrites the original one.

In your particular case, func1() returns 2 and func2() returns 3, as these are values returned in the finally blocks.

2 of 4
61

It will always go to the finally block, so it will ignore the return in the try and except. If you would have a return above the try and except, it would return that value.

def func1():
    try:
        return 1 # ignoring the return
    finally:
        return 2 # returns this return

def func2():
    try:
        raise ValueError()
    except:
        # is going to this exception block, but ignores the return because it needs to go to the finally
        return 1
    finally:
        return 3

def func3():
    return 0 # finds a return here, before the try except and finally block, so it will use this return 
    try:
        raise ValueError()
    except:
        return 1
    finally:
        return 3


func1() # returns 2
func2() # returns 3
func3() # returns 0
Discussions

Python confusion with return value in try-except-finally - Stack Overflow
Here is a piece of my code: def main(): num = 0 try: raise Exception('This is the error message.') except Exception: num += 1 return num finally: num... More on stackoverflow.com
🌐 stackoverflow.com
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
3
0
December 6, 2023
Python function with try, except, finally, and return
As we have observed in instructional content, technical literature, and official Python documentation, a return statement, when executed, terminates execution of the function that contains it, and delivers a value to the statement that called the function. Literature on Python also offers much ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
9
August 23, 2020
What will happen in this try except?
> Swallowing an exception like this without any warning feels dangerous. Then don't swallow the exception. This is all documented very explicitly and clearly: https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions Your exception would have been re-raised after the finally clause except that you put a return in it, triggering this case: > If the finally clause executes a break, continue, or return statement, exceptions are not re-raised. The point of a finally clause is to clean up resources from within the try clause, don't abuse it for unnecessary control flow. More on reddit.com
🌐 r/Python
25
0
January 13, 2025
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.7 documentation
An exception could occur during execution of an except or else clause. Again, the exception is re-raised after the finally clause has been executed. If the finally clause executes a break, continue or return ...
🌐
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
🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
You can use the else keyword to ... went wrong") Try it Yourself » · The finally block, if specified, will be executed regardless if the try block raises an error or not....
🌐
Astral
docs.astral.sh › ruff › rules › return-in-try-except-finally
return-in-try-except-finally (SIM107) | Ruff - Astral Docs
def squared(n): try: return_value = n**2 except Exception: return_value = "An exception occurred" finally: return_value = -1 return return_value · Python documentation: Defining Clean-up Actions Back to top
🌐
Plain English
plainenglish.io › home › blog › python › caveats of using return with try/except in python
Caveats of using return with try/except in Python
September 3, 2020 - We should remember the fact that a finally statement gets executed ALWAYS. To have a clearer idea of the execution flow, let’s add print statements in every block. def test_func(): try: x = 10 print(f" Inside try block ") return x except Exception as e: x = 20 print(f" Inside except block ") return x finally: x = 30 print(f" Inside finally block ") return x print(test_func()) Output: Inside try block Inside finally block 30
Find elsewhere
🌐
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…
🌐
Codecademy Forums
discuss.codecademy.com › community › town square
Python function with try, except, finally, and return - Town Square - Codecademy Forums
August 23, 2020 - As we have observed in instructional content, technical literature, and official Python documentation, a return statement, when executed, terminates execution of the function that contains it, and delivers a value to the statement that called the function. Literature on Python also offers much information on handling exceptions.
🌐
Medium
medium.com › analytics-vidhya › do-you-really-understand-try-finally-in-python-110cee4c1a8
Do You Really Understand Try & Finally in Python? | by Aravind Ramalingam | Analytics Vidhya | Medium
March 14, 2024 - Even if there’s an error in an exception handler or the else-block and a new exception is raised, the code in the final-block is still run. This quote from the python documentation is absolutely correct but the execution behavior is little tricky when try and finally blocks are encapsulated within a function which has a return statement.
🌐
Raravind
raravind.com › blog › data-science › do-you-really-understand-try-finally-in-python
Do You Really Understand Try & Finally in Python?
April 18, 2021 - Even if there's an error in an exception handler or the else-block and a new exception is raised, the code in the final-block is still run. This quote from the python documentation is absolutely correct but the execution behavior is little tricky when try and finally blocks are encapsulated within a function which has a return statement.
🌐
InventiveHQ
inventivehq.com › home › blog › software engineering › python try except: complete error handling guide
Python Try Except: Complete Error Handling Guide
July 18, 2026 - Second, finally runs even when try contains a return; Python holds the return value, runs finally, then returns. If finally has its own return, it wins and silently discards the earlier value or exception — a classic footgun.
Address: 2305 Historic Decatur Rd, Suite 100, 92106, San Diego
🌐
Reddit
reddit.com › r/python › what will happen in this try except?
r/Python on Reddit: What will happen in this try except?
January 13, 2025 -
def func():
    try:
        a = 1 / 0  # This will raise a ZeroDivisionError
        return a
    except ZeroDivisionError as e:
        raise e  # Reraise the exception
        return -1
    finally:
        return "finally"

The answer is: the function returns 'finally'

In Python, the finally block always executes, regardless of whether there’s an exception, a return statement, or anything else happening in the try or except blocks. If the finally block contains a return statement, it takes precedence over everything else, including exceptions and other return statements. Swallowing an exception like this without any warning feels dangerous.

Do you think this expected behavior? How do other programming languages handle this?

🌐
Real Python
realpython.com › lessons › python-return-try-finally
Using return With try and finally Blocks (Video) – Real Python
If it is unsuccessful, then a ValueError will occur, and the return statement in the except clause will be executed, which returns the argument in values as a string. 02:13 In either case, since this try statement has a finally clause, it will be executed before the function returns.
Published: August 10, 2021
🌐
Ancisoft
ancisoft.com › blog › weird-try-except-else-finally-behavior-with-return-statements
Weird Python Try-Except-Else-Finally Behavior with Return Statements: Why Your Code Outputs Unexpected Results — ancisoft.com
else runs only if try completes normally (no exceptions, return, break, or continue). finally always runs, even after return in other blocks. A return in finally overrides all previous return values.
🌐
Reddit
reddit.com › r/python › try... except... finally!
r/Python on Reddit: try... except... finally!
May 21, 2024 - The finally block is executed when you leave the try and except blocks. So, if you or Python leaves those blocks by a return statement, an exit function call, or by any exception, whether caught or not, the code in the finally block will be 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
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.