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

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
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
exceptions - Using a try-finally (without catch) vs enum-state validation - Software Engineering Stack Exchange
I have been reading the advice on this question about how an exception should be dealt with as close to where it is raised as possible. My dilemma on the best practice is whether one should use a ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
February 15, 2012
exception - What is the intended use of the optional "else" clause of the "try" statement in Python? - Stack Overflow
It does require at least one preceding except clause (see the grammar). So it really isn't "try-else," it's "try-except-else(-finally)," with the else (and finally) being optional. The Python Tutorial elaborates on the intended usage: More on stackoverflow.com
🌐 stackoverflow.com
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.

🌐
Qpython
qpython.com › python-try-without-except-1h9a
Python try Without except – QPython+
January 12, 2026 - How can you structure error handling so that success paths and cleanup run smoothly without catching every exception in the same block? By combining try with else and finally, you can keep error-handling code distinct from normal execution and cleanup logic. The else block runs only when no ...
🌐
Medium
medium.com › towardsdev › try-except-finally-python-how-to-deal-with-exceptions-2c428e0372c8
Try, Except & Finally (Python)— How To Deal With Exceptions | by Liu Zuo Lin | Towards Dev
August 12, 2022 - Sometimes we want to catch specific exceptions and handle them differently. Here’s how we do it: try: # some risky codeexcept ZeroDivisionError as err: print(err) print("cannot divide by 0")except TypeError as err: print(err) print("type…
🌐
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
Find elsewhere
🌐
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
🌐
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
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.5 documentation
If an exception occurs during execution of the try clause, the exception may be handled by an except clause. If the exception is not handled by an except clause, the exception is re-raised after the finally clause has been executed.
🌐
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") ...
🌐
SmartBear Community
community.smartbear.com › smartbear community › testcomplete › testcomplete questions
Python Error Handling Not Executing Finally Block - TestComplete 14.0 | SmartBear Community
It appears that Python's try/except/finally overrides all TC settings & worse, masks other exceptions/call stack. At least that's the behavior I see.
🌐
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…
🌐
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.
🌐
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.")
🌐
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
You can use the try statement in any of the following forms: try-catch - to handle exceptions that might occur during execution of the code inside a try block, try-finally - to specify the code that runs when control leaves the try block, and ...
Top answer
1 of 8
16

Exceptions should be used for exceptional conditions. Throwing an exception is basically making the statement, "I can't handle this condition here; can someone higher up on the call stack catch this for me and handle it?"

Returning a value can be preferable, if it is clear that the caller will take that value and do something meaningful with it. This is especially true if throwing an exception has performance implications, i.e. it may occur in a tight loop. Throwing an exception takes much longer than returning a value (by at least two orders of magnitude).

Exceptions should never be used to implement program logic. In other words, don't throw an exception to get something done; throw an exception to state that it couldn't be done.

2 of 8
4

Some good advice I once read was, throw exceptions when you cannot progress given the state of the data you are dealing with, however if you have a method which may throw an exception, also provide where possible a method to assert whether the data is actually valid before the method is called.

For example, System.IO.File.OpenRead() will throw a FileNotFoundException if the file supplied does not exist, however it also provides a .Exists() method which returns a boolean value indicating whether the file is present which you should call before calling OpenRead() to avoid any unexpected exceptions.

To answer the "when should I deal with an exception" part of the question, I would say wherever you can actually do something about it. If your method cannot deal with an exception thrown by a method it calls, don't catch it. Let it raise higher up the call chain to something that can deal with it. In some cases, this may just be a logger listening to Application.UnhandledException.

🌐
AskPython
askpython.com › home › exception handling python: try-except mastery
Exception Handling Python: Try-Except Mastery - AskPython
January 25, 2026 - Placing this code in the try block ... handler. The else clause prevents this confusion. The finally block always executes, regardless of whether an exception occurred....
Top answer
1 of 16
1152

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.

🌐
Quora
quora.com › What-is-the-purpose-of-the-try-except-else-finally-block-in-Python
What is the purpose of the 'try-except-else-finally' block in Python? - Quora
Well, the “try-except-finally” block is used to catch exceptions or errors in your program which you might miss or do not know where it is going to occur. It saves your program from unexpected closure and help you close ...