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
Answer from Blair Conrad on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.3 documentation
The try … except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.
🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
try: print(x) except NameError: print("Variable x is not defined") except: print("Something else went wrong") Try it Yourself » · See more Error types in our Python Built-in Exceptions Reference.
Discussions

exception - What is the intended use of the optional "else" clause of the "try" statement in Python? - Stack Overflow
Also keep in mind that variables used in the try-block CAN be used in the else-block, so you should alway consider using this variant if you don't expect more exceptions in the else-block 2014-08-06T11:56:20.24Z+00:00 ... There's no such thing as a "try-scoped variable". In Python, variable ... More on stackoverflow.com
🌐 stackoverflow.com
exception - Is it a good practice to use try-except-else in Python? - Stack Overflow
From time to time in Python, I see the block: try: try_this(whatever) except SomeException as exception: #Handle exception else: return something What is the reason for the try-except-els... More on stackoverflow.com
🌐 stackoverflow.com
Try Except Else question- when do you use Else?
As u/OskaRRRitoS said already: else is for running code if no exception is raised. Try to think of it as a way to minimize your try block. Sometimes you want to do additional stuff as a part of the try block, but you want to keep the try block as minimal and specific as possible. So you can put things into else that have to happen if try doesn't fail, but doesn't need to be caught by except. More on reddit.com
🌐 r/Python
48
35
October 15, 2022
Replacement for `else` keyword in `try-else` (for ex. `noexcept` or `not except`) - Ideas - Discussions on Python.org
The issue I will start with from where I got this idea. During discussion about elif in compound statements some of the points were about how keyword else is often unintuitive. Here are some summaries of understanding the keyword:. I always understood it differently. More on discuss.python.org
🌐 discuss.python.org
0
August 1, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › try-except-else-and-finally-in-python
Try, Except, else and Finally in Python - GeeksforGeeks
The code enters the else block only if the try clause does not raise an exception. Example: Else block will execute only when no exception occurs. ... # Python code to illustrate working of try() def divide(x, y): try: # Floor Division : Gives ...
Published   July 15, 2025
Top answer
1 of 16
1149

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.

Top answer
1 of 11
871

"I do not know if it is out of ignorance, but I do not like that kind of programming, as it is using exceptions to perform flow control."

In the Python world, using exceptions for flow control is common and normal.

Even the Python core developers use exceptions for flow-control and that style is heavily baked into the language (i.e. the iterator protocol uses StopIteration to signal loop termination).

In addition, the try-except-style is used to prevent the race-conditions inherent in some of the "look-before-you-leap" constructs. For example, testing os.path.exists results in information that may be out-of-date by the time you use it. Likewise, Queue.full returns information that may be stale. The try-except-else style will produce more reliable code in these cases.

"It my understanding that exceptions are not errors, they should only be used for exceptional conditions"

In some other languages, that rule reflects their cultural norms as reflected in their libraries. The "rule" is also based in-part on performance considerations for those languages.

The Python cultural norm is somewhat different. In many cases, you must use exceptions for control-flow. Also, the use of exceptions in Python does not slow the surrounding code and calling code as it does in some compiled languages (i.e. CPython already implements code for exception checking at every step, regardless of whether you actually use exceptions or not).

In other words, your understanding that "exceptions are for the exceptional" is a rule that makes sense in some other languages, but not for Python.

"However, if it is included in the language itself, there must be a good reason for it, isn't it?"

Besides helping to avoid race-conditions, exceptions are also very useful for pulling error-handling outside loops. This is a necessary optimization in interpreted languages which do not tend to have automatic loop invariant code motion.

Also, exceptions can simplify code quite a bit in common situations where the ability to handle an issue is far removed from where the issue arose. For example, it is common to have top level user-interface code calling code for business logic which in turn calls low-level routines. Situations arising in the low-level routines (such as duplicate records for unique keys in database accesses) can only be handled in top-level code (such as asking the user for a new key that doesn't conflict with existing keys). The use of exceptions for this kind of control-flow allows the mid-level routines to completely ignore the issue and be nicely decoupled from that aspect of flow-control.

There is a nice blog post on the indispensibility of exceptions here.

Also, see this Stack Overflow answer: Are exceptions really for exceptional errors?

"What is the reason for the try-except-else to exist?"

The else-clause itself is interesting. It runs when there is no exception but before the finally-clause. That is its primary purpose.

Without the else-clause, the only option to run additional code before finalization would be the clumsy practice of adding the code to the try-clause. That is clumsy because it risks raising exceptions in code that wasn't intended to be protected by the try-block.

The use-case of running additional unprotected code prior to finalization doesn't arise very often. So, don't expect to see many examples in published code. It is somewhat rare.

Another use-case for the else-clause is to perform actions that must occur when no exception occurs and that do not occur when exceptions are handled. For example:

recip = float('Inf')
try:
    recip = 1 / f(x)
except ZeroDivisionError:
    logging.info('Infinite result')
else:
    logging.info('Finite result')

Another example occurs in unittest runners:

try:
    tests_run += 1
    run_testcase(case)
except Exception:
    tests_failed += 1
    logging.exception('Failing test case: %r', case)
    print('F', end='')
else:
    logging.info('Successful test case: %r', case)
    print('.', end='')

Lastly, the most common use of an else-clause in a try-block is for a bit of beautification (aligning the exceptional outcomes and non-exceptional outcomes at the same level of indentation). This use is always optional and isn't strictly necessary.

2 of 11
215

What is the reason for the try-except-else to exist?

A try block allows you to handle an expected error. The except block should only catch exceptions you are prepared to handle. If you handle an unexpected error, your code may do the wrong thing and hide bugs.

An else clause will execute if there were no errors, and by not executing that code in the try block, you avoid catching an unexpected error. Again, catching an unexpected error can hide bugs.

Example

For example:

try:
    try_this(whatever)
except SomeException as the_exception:
    handle(the_exception)
else:
    return something

The "try, except" suite has two optional clauses, else and finally. So it's actually try-except-else-finally.

else will evaluate only if there is no exception from the try block. It allows us to simplify the more complicated code below:

no_error = None
try:
    try_this(whatever)
    no_error = True
except SomeException as the_exception:
    handle(the_exception)
if no_error:
    return something

so if we compare an else to the alternative (which might create bugs) we see that it reduces the lines of code and we can have a more readable, maintainable, and less buggy code-base.

finally

finally will execute no matter what, even if another line is being evaluated with a return statement.

Broken down with pseudo-code

It might help to break this down, in the smallest possible form that demonstrates all features, with comments. Assume this syntactically correct (but not runnable unless the names are defined) pseudo-code is in a function.

For example:

try:
    try_this(whatever)
except SomeException as the_exception:
    handle_SomeException(the_exception)
    # Handle a instance of SomeException or a subclass of it.
except Exception as the_exception:
    generic_handle(the_exception)
    # Handle any other exception that inherits from Exception
    # - doesn't include GeneratorExit, KeyboardInterrupt, SystemExit
    # Avoid bare `except:`
else: # there was no exception whatsoever
    return something()
    # if no exception, the "something()" gets evaluated,
    # but the return will not be executed due to the return in the
    # finally block below.
finally:
    # this block will execute no matter what, even if no exception,
    # after "something" is eval'd but before that value is returned
    # but even if there is an exception.
    # a return here will hijack the return functionality. e.g.:
    return True # hijacks the return in the else clause above

It is true that we could include the code in the else block in the try block instead, where it would run if there were no exceptions, but what if that code itself raises an exception of the kind we're catching? Leaving it in the try block would hide that bug.

We want to minimize lines of code in the try block to avoid catching exceptions we did not expect, under the principle that if our code fails, we want it to fail loudly. This is a best practice.

It is my understanding that exceptions are not errors

In Python, most exceptions are errors.

We can view the exception hierarchy by using pydoc. For example, in Python 2:

$ python -m pydoc exceptions

or Python 3:

$ python -m pydoc builtins

Will give us the hierarchy. We can see that most kinds of Exception are errors, although Python uses some of them for things like ending for loops (StopIteration). This is Python 3's hierarchy:

BaseException
    Exception
        ArithmeticError
            FloatingPointError
            OverflowError
            ZeroDivisionError
        AssertionError
        AttributeError
        BufferError
        EOFError
        ImportError
            ModuleNotFoundError
        LookupError
            IndexError
            KeyError
        MemoryError
        NameError
            UnboundLocalError
        OSError
            BlockingIOError
            ChildProcessError
            ConnectionError
                BrokenPipeError
                ConnectionAbortedError
                ConnectionRefusedError
                ConnectionResetError
            FileExistsError
            FileNotFoundError
            InterruptedError
            IsADirectoryError
            NotADirectoryError
            PermissionError
            ProcessLookupError
            TimeoutError
        ReferenceError
        RuntimeError
            NotImplementedError
            RecursionError
        StopAsyncIteration
        StopIteration
        SyntaxError
            IndentationError
                TabError
        SystemError
        TypeError
        ValueError
            UnicodeError
                UnicodeDecodeError
                UnicodeEncodeError
                UnicodeTranslateError
        Warning
            BytesWarning
            DeprecationWarning
            FutureWarning
            ImportWarning
            PendingDeprecationWarning
            ResourceWarning
            RuntimeWarning
            SyntaxWarning
            UnicodeWarning
            UserWarning
    GeneratorExit
    KeyboardInterrupt
    SystemExit

A commenter asked:

Say you have a method which pings an external API and you want to handle the exception at a class outside the API wrapper, do you simply return e from the method under the except clause where e is the exception object?

No, you don't return the exception, just reraise it with a bare raise to preserve the stacktrace.

try:
    try_this(whatever)
except SomeException as the_exception:
    handle(the_exception)
    raise

Or, in Python 3, you can raise a new exception and preserve the backtrace with exception chaining:

try:
    try_this(whatever)
except SomeException as the_exception:
    handle(the_exception)
    raise DifferentException from the_exception

I elaborate in my answer here.

🌐
Reddit
reddit.com › r/python › try except else question- when do you use else?
r/Python on Reddit: Try Except Else question- when do you use Else?
October 15, 2022 -

My understanding is that Else runs if Try succeeds without any exceptions. What are the uses for this where you couldn’t just put that code in the Try statement?

🌐
Server Academy
serveracademy.com › blog › python-try-except
Python Try Except - Server Academy
Here, try and except are used to ... except blocks as needed. In Python, the else block is used with try and except to define code that runs only if no exceptions occur in the try block....
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
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
December 1, 2024 - except allows you to catch and handle the exception or exceptions that Python encountered in the try clause. else lets you code sections that should run only when Python encounters no exceptions in the try clause.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Try, except, else, finally in Python (Exception handling) | note.nkmk.me
August 15, 2023 - If an exception does occur and is caught by an except clause, the action in the else clause will not be executed. def divide_else(a, b): try: print(a / b) except ZeroDivisionError as e: print('catch ZeroDivisionError:', e) else: print('finish ...
🌐
freeCodeCamp
freecodecamp.org › news › error-handling-in-python-introduction
Error Handling in Python – try, except, else, & finally Explained with Code Examples
November 6, 2025 - x = 0 try: print(5 / x) except ZeroDivisionError: print("I am the except clause!") else: print("I am the else clause!") finally: print("I am the finally clause!") print("I am executing after the try clause!") # I am the except clause! # I am the finally clause! # I am executing after the try clause! You’ve seen me write about two different named exceptions so far: NameError and ZeroDivisionError. What if I needed other exceptions? There is an entire list of Python’s exceptions that come with the standard library.
🌐
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
# -*- coding: utf-8 -*- try: a = input("Enter first number: ") b = input("Enter second number: ") result = int(a)/int(b) except (ValueError, ZeroDivisionError): print("Something went wrong...") else: print("Result is squared: ", result``2) ... $ python divide_ver3.py Enter first number: 10 Enter second number: 2 Result is squared: 25 $ python divide_ver3.py Enter first number: werq Enter second number: 3 Something went wrong...
🌐
Python Tutorial
pythontutorial.net › home › python basics › python try…except…else
Python try...except...else Statement Explained By Practical Examples
January 12, 2026 - The try...except...else statement works as follows: If an exception occurs in the try clause, Python skips the rest of the statements in the try clause and the except statement execute.
🌐
GUVI
guvi.in › hub › python › try-except-else-in-python
try…except…else in Python
February 12, 2016 - In conclusion, the 'try...except...else' statement in Python offers a powerful mechanism for handling exceptions while also executing code that should run only if no exceptions are raised.
🌐
Python Basics
pythonbasics.org › try-except
Try and Except in Python - Python Tutorial
November 1, 2023 - A try-except block can have the finally clause (optionally). The finally clause is always executed. So the general idea is: For instance: if you open a file you’ll want to close it, you can do so in the finally clause. The else clause is executed if and only if no exception is raised.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-try-except
Python Try Except - GeeksforGeeks
July 23, 2025 - try: # Some Code except: # Executed if error in the # try block else: # execute if no exception finally: # Some code .....(always executed) ... # Python program to demonstrate finally # No exception Exception raised in try block try: k = 5//0 # raises divide by zero exception.
🌐
Python.org
discuss.python.org › ideas
Replacement for `else` keyword in `try-else` (for ex. `noexcept` or `not except`) - Ideas - Discussions on Python.org
August 1, 2024 - The issue I will start with from where I got this idea. During discussion about elif in compound statements some of the points were about how keyword else is often unintuitive. Here are some summaries of understanding th…
🌐
Board Infinity
boardinfinity.com › blog › try-except-in-python
Try, Except, Else and Finally in Python | Board Infinity
August 14, 2025 - If the try block successfully does not raise an exception then the code enters into the else block. ... In Python, we use the final keyword, which is executed always even if the exception is not handled and the try-except block is terminated.
🌐
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 - According to the Python manual, using the else clause is better than adding additional code to the try clause. But why? The reasoning is that it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try and except statements in the first place.
🌐
Nim Forum
forum.nim-lang.org › t › 9034
try except else
March 1, 2022 - We cannot provide a description for this page right now