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
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
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.4 documentation
If a finally clause includes a return statement, the returned value will be the one from the finally clause’s return statement, not the value from the try clause’s return statement. This can be confusing and is therefore discouraged.
Discussions

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 - Weird Try-Except-Else-Finally behavior with Return statements - Stack Overflow
This is some code that is behaving peculiarly. This is a simplified version of the behavior that I've written. This will still demonstrate the weird behavior and I had some specific questions on wh... More on stackoverflow.com
🌐 stackoverflow.com
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
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
🌐
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.

🌐
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
🌐
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....
🌐
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 as Answer result = x // y except ZeroDivisionError: print("Sorry !
Published   July 15, 2025
Find elsewhere
🌐
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
🌐
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.
🌐
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
Even if you return from the except, the finally code is run. If you also return from the finally, that value is what’s returned. It’s handy for freeing up resources that may not be released when Python garbage-collects things.
🌐
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 - The variable val has value 1 in try-block and gets updated to 2 in final-block. # The try block has return statement & final block has only print statement def example_2(): try: val = 1 print(f"Print: Try Block - ") return f"Return: Try Block - " finally: val = val + 1 print(f"Print: Finally Block - ") example_2()
🌐
Justin Joyce
justinjoyce.dev › home › latest › python try except
Python try except | Justin Joyce
February 18, 2024 - A return statement within a finally block is always the value returned from its function, even if there’s an unhandled exception in its try / except blocks.
🌐
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 ...
🌐
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 ...
🌐
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 ...
Published   August 10, 2021
🌐
Inventive HQ
inventivehq.com › home › blog › automation › python try except: complete error handling guide
Python Error Handling: Try, Except, Finally & With Guide
November 6, 2025 - Except Block: Handles specific exceptions when they occur in the try block · Finally Block: Executes regardless of whether an exception occurred, perfect for cleanup operations · In enterprise applications, you’ll encounter various error types. Python allows you to handle multiple exceptions with specific responses for each scenario: