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()) Answer from Deleted User on reddit.com
🌐
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") ...
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.

🌐
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 documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.4 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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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_ver4.py Enter first number: 4 Enter second number: 0 Something went wrong... And they lived happily ever after. As a rule, same code can be written with or without exceptions. ... while True: a = input("Enter first number: ") b = input("Enter second number: ") try: result = int(a)/int(b) except ValueError: print("Only digits are supported") except ZeroDivisionError: print("You can't divide by zero") else: print(result) break
🌐
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…
🌐
GUVI
guvi.in › hub › python › try-except-finally-in-python
try…except…finally in Python
This example demonstrates how 'try...except...finally' allows you to handle exceptions and execute critical code in the 'finally' block, ensuring that necessary cleanup or resource management tasks are always performed, even in the presence of exceptions. To conclude, the try...except statement in Python provides a robust and comprehensive approach to exception handling and resource management.
🌐
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 ...
🌐
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 - The try statement fails -> The except statement runs -> The else statement is skipped -> The finally statement runs · The idea in my example above is to write the div text to a file only if it’s found on the page. It’s important to do this after the try block runs, to avoid the except block catching any file handling error that may arise.
🌐
W3Schools
w3schools.com › python › gloss_python_try_finally.asp
Python Try Finally
Python Examples Python Compiler ... Q&A Python Bootcamp Python Training ... The finally block, if specified, will be executed regardless if the try block raises an error or not....
🌐
Note.nkmk.me
note.nkmk.me › home › python
Try, except, else, finally in Python (Exception handling) | note.nkmk.me
August 15, 2023 - 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 · def divide_exception(a, b): try: print(a / b) except Exception as e: print(e) divide_exception(1, 0) # division by zero divide_exception('a', 'b') # unsupported operand type(s) for /: 'str' and 'str'
🌐
Python Tutorial
pythontutorial.net › home › python basics › python try…except…finally
Python try...except...finally Statement - Python Tutorial
March 30, 2025 - So you can write it like this: ... to close the file that has been opened. Use Python try...except...finally statement to execute a code block whether an exception occurs or not....
🌐
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.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 ...
🌐
freeCodeCamp
freecodecamp.org › news › error-handling-in-python-introduction
Error Handling in Python – try, except, else, & finally Explained with Code Examples
April 11, 2024 - The short answer is no. With the release of Python 3.11, there is practically no speed reduction from using try and except statements when there are no thrown exceptions. Catching errors did cause some slowdowns.