You can find documented in the section about the try statement:

If the finally clause executes a return, break or continue statement, the saved exception is discarded.

Of course, the fact that it's a documented behavior does not entirely explain the reasoning, so I offer this: a function can only exit in one of two ways, by returning a value or raising an exception. It can not do both.

Since the finally block is commonly used as a cleanup handler, it makes sense for the return to take priority here. You do have the chance to re-raise any exceptions within finally, simply by not using a return statement.

Answer from platypus on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › is it okay to use try/finally without except?
r/learnpython on Reddit: Is it okay to use try/finally without except?
February 12, 2016 -

Hi and sorry for the noob question,

I am calling another python script as a subprocess. The python script tries to do X, and if/when it fails it MUST do Y. I had originally been handling this by doing the following:

 try:
    x
except:
   <code to be run>

For what I want to do, would it be better to simply use:

try:
    x
finally:
    y

If I understand correctly, I am basically using except for something finally should be used for at the moment, yes? Is there any downside to not using except for what I want to accomplish?

Top answer
1 of 4
8
Code after finally will be called whether the code after the try works or not. It doesn't care. Gets called every time. If you need something to run IF AND ONLY IF the code in the try FAILS, then you need to use except.
2 of 4
1
try will attempt a piece of code. If that results in exception, you can capture and handle that exception within except. Whether or not an exception occurred, you can run code in a finally block to guarantee that it runs in all circumstances. That way, even if the exception stops the program, code in the finally block will still execute. For your purpose, you may want to have all three in place: try: # try this code which may cause an Exception except AThingHappened as e: # handle the exception finally: # code that occurs whether exception happened or not. When you run code in the except block, it's possible to prevent that exception from stopping your program. This is a good way to handle common errors, such as the one you're expecting when you run your subscript. If you want, you can choose to raise the exception again from inside the except block, which lets it bubble up through the program until something else catches it or the thread ceases execution. In either case, any code in finally will be run. This is useful for some code that might do some clean up, like closing a connection. To more directly answer your question, if you omit the except block, your code in finally will still run, but you won't be handling the exception. Instead, that exception will be raised and you'll see its traceback in the console. If you have some logic you want to use to handle that exception, use an except statement. However, depending on your use-case, you might do better designing something that can work with the with statement . I don't know the specifics of your program, so I can't say if it would be helpful, but it's worth considering before trying to make something that's overly complex using try..except..finally.
Discussions

python - try; finally without except? - Stack Overflow
So if some_cursor.execute(sql_query) ... outer try/except block as @Barmar pointed out in the comments). While some database libraries and Python database drivers may automatically close resources when the program exits, relying on this behaviour is not recommended because it can lead to resource leaks and other issues. finally contains your ... More on stackoverflow.com
🌐 stackoverflow.com
language agnostic - Why use try … finally without a catch clause? - Software Engineering Stack Exchange
Care should be taken in the finally ... throw an exception. For example, be doubly sure to check all variables for null, etc. ... +1: It's idiomatic for "must be cleaned up". Most uses of try-finally can be replaced with a with statement. ... Various languages have extremely useful language-specific enhancements to the try/finally construct. C# has using, Python has with, ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
January 23, 2012
For ... except ... finally - Ideas - Discussions on Python.org
While using the instruction for on a generator, I wanted to catch an exception coming from the generator, and I didn’t found a better solution to use a verbose try except around the for, and which becomes complicated when several errors can come from both iteration and content codes. More on discuss.python.org
🌐 discuss.python.org
0
January 22, 2024
exception - Why do we need the “finally:” clause in Python, if we can just write code after the “try:” block? - Stack Overflow
Try running this code first without a finally block, ... For the first case you don't have a finally block, So when an error occurs in the except block the program execution halts and you cannot execute anything after the except block. But for the second case, The error occurs but before the program halts python ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.5 documentation
If the finally clause executes a break, continue or return statement, exceptions are not re-raised. This can be confusing and is therefore discouraged. From version 3.14 the compiler emits a SyntaxWarning for it (see PEP 765). If the try statement reaches a break, continue or return statement, the finally clause will execute just prior to the break, continue or return statement’s execution.
🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
You can use the else keyword to define a block of code to be executed if no errors were raised: In this example, the try block does not generate any error: try: print("Hello") except: print("Something went wrong") else: print("Nothing went wrong") ...
🌐
Qpython
qpython.com › python-try-without-except-1h9a
Python try Without except – QPython+
January 12, 2026 - We call .raise_for_status() inside the try so HTTP errors become exceptions. In else, we assume the request succeeded and parse JSON. In finally, we log completion regardless of outcome. Quick scripts: For throwaway code where cleanup matters but detailed error handling doesn’t. Resource cleanup: Files, locks, network sockets. Transparent failures: You want errors to halt execution and be visible. Using try without except may seem unconventional, but it clarifies your program’s structure by cleanly separating error handling, successful execution, and cleanup.
Top answer
1 of 9
182

It depends on whether you can deal with the exceptions that can be raised at this point or not.

If you can handle the exceptions locally you should, and it is better to handle the error as close to where it is raised as possible.

If you can't handle them locally then just having a try / finally block is perfectly reasonable - assuming there's some code you need to execute regardless of whether the method succeeded or not. For example (from Neil's comment), opening a stream and then passing that stream to an inner method to be loaded is an excellent example of when you'd need try { } finally { }, using the finally clause to ensure that the stream is closed regardless of the success or failure of the read.

However, you will still need an exception handler somewhere in your code - unless you want your application to crash completely of course. It depends on the architecture of your application exactly where that handler is.

2 of 9
40

The finally block is used for code that must always run, whether an error condition (exception) occurred or not.

The code in the finally block is run after the try block completes and, if a caught exception occurred, after the corresponding catch block completes. It is always run, even if an uncaught exception occurred in the try or catch block.

The finally block is typically used for closing files, network connections, etc. that were opened in the try block. The reason is that the file or network connection must be closed, whether the operation using that file or network connection succeeded or whether it failed.

Care should be taken in the finally block to ensure that it does not itself throw an exception. For example, be doubly sure to check all variables for null, etc.

Find elsewhere
🌐
Server Academy
serveracademy.com › blog › python-try-except
Python Try Except - Blog - ServerAcademy.com
When writing Python code, errors can be frustrating. Luckily, Python’s and blocks provide an effective way to manage errors without breaking the flow of your co
🌐
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.")
🌐
DevTechie
devtechie.com › blog › python-try-except-else-and-try-finally
Python: try … except … else and try … finally
Oh! Do not be confused, else block and finally block are not mutually exclusive. You can use both else and finally with the same try ... except block since they serve different purposes. else block executes when try block runs without an error whereas finally block runs always.
🌐
GeeksforGeeks
geeksforgeeks.org › python › finally-keyword-in-python
Finally keyword in Python - GeeksforGeeks
5 days ago - This example shows that the finally block executes even when no exception occurs. ... Explanation: The try block executes without any errors, so the except block is skipped.
🌐
Python.org
discuss.python.org › ideas
For ... except ... finally - Ideas - Discussions on Python.org
January 22, 2024 - While using the instruction for on a generator, I wanted to catch an exception coming from the generator, and I didn’t found a better solution to use a verbose try except around the for, and which becomes complicated whe…
🌐
GeeksforGeeks
geeksforgeeks.org › python › try-except-else-and-finally-in-python
Try, Except, else and Finally in Python - GeeksforGeeks
try: # Some Code.... except: # optional block # Handling of exception (if required) else: # execute if no exception finally: # Some code .....(always executed) Let’s first understand how the Python try and except works
Published   July 15, 2025
🌐
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
$ python divide_ver2.py Enter first number: 5 Enter second number: 0 Something went wrong... ... In block except you don’t have to specify a specific exception or exceptions. In that case, all exceptions would be intercepted. That is not recommended! Try/except has an optional else block.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Try, except, else, finally in Python (Exception handling) | note.nkmk.me
August 15, 2023 - Using a wildcard except, you can catch all exceptions, including SystemExit (raised by sys.exit(), etc.) and KeyboardInterrupt (triggered by pressing Ctrl + C). However, it's often preferable not to catch these particular exceptions. In such cases, using Exception instead may be a better option, as described next. You can specify Exception in the except clause, which is the base class for all built-in, non-system-exiting exceptions. Built-in Exceptions - Exception — Python 3.11.3 documentation
🌐
Programiz
programiz.com › python-programming › exception-handling
Python Exception Handling (With Examples)
Here's the syntax of try...except ... is followed by an except block. When an exception occurs, it is caught by the except block. The except block cannot be used without the try block....
🌐
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
🌐
Python Land
python.land › home › language deep dives › python try except: examples and best practices
Python Try Except: Examples And Best Practices • Python Land Tutorial
January 29, 2026 - The finally block is executed regardless of whether an exception occurs or not. Finally blocks are useful, for example, when you want to close a file or a network connection regardless of what happens.
🌐
Python.org
discuss.python.org › python help
Why aren't exceptions re-raised if `finally` contains a `return/break/continue`? - Python Help - Discussions on Python.org
August 11, 2022 - From the docs: If the finally clause executes a break, continue or return statement, exceptions are not re-raised. Why is this? I would have thought that an exception would take priority over a return/break/continue statement, especially since PEP8 explicitly discourages the use of control flow statements inside finally (see here): Use of the flow control statements return /break /continue within the finally suite of a try...finally , where the flow control statement would jump outside the...