The following sample code shows you how to catch and ignore an exception, using pass.

try:
    do_something()
except RuntimeError:
    pass # does nothing
else:
    print("Message: ", line) 
Answer from Jochen Ritzel on Stack Overflow
🌐
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
🌐
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?

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
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
try except else
Sign up · Log in · Reset your password · Create account · Reset password · Create a new account More on forum.nim-lang.org
🌐 forum.nim-lang.org
python - How to properly ignore exceptions - Stack Overflow
Finally a good explanation of else in this context. And to add that finally will always run after any (or no exception). 2018-10-29T14:50:52.407Z+00:00 ... Save this answer. ... Show activity on this post. When you just want to do a try catch without handling the exception, how do you do it in Python... More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
Python Examples Python Compiler ... Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no err...
🌐
Qpython
qpython.com › python-try-without-except-1h9a
Python try Without except – QPython+
January 12, 2026 - try: value = int(user_input) else: if value < 0: raise ValueError("Negative numbers not allowed") finally: print("Input processing done.") For more on crafting custom error messages, check out raising exceptions with custom messages in Python.
Top answer
1 of 16
1156

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.

🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.7 documentation
If the exception is not handled by an except clause, the exception is re-raised after the finally clause has been executed. An exception could occur during execution of an except or else ...
Find elsewhere
🌐
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…
🌐
GUVI
guvi.in › hub › python › try-except-else-in-python
try…except…else in Python
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. The 'else' block provides a convenient way to define code that is executed when the 'try' block completes successfully without any exceptions.
🌐
Medium
s16h.medium.com › the-optional-else-in-pythons-try-statement-deb0079212e8
The Optional `else` in Python’s `try` Statement | by Shahriar Tajbakhsh | Medium
April 28, 2020 - Also, without the else clause, the only option to run additional code before finalisation (which is rare) would be the clumsy practice of adding the code to the try clause. This is clumsy because it risks raising exceptions in code that wasn’t intended to be protected by the try statement. In general, in Python, there is probably not a case where you have no choice but to use the else clause in a try statement.
🌐
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.
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
March 18, 2026 - While using try together with except is probably the most common error handling that you’ll encounter, there’s more that you can do to fine-tune your program’s response to exceptions. ... You can use Python’s else statement to instruct a program to execute a certain block of code only in the absence of exceptions:
🌐
Uci
tutors.ics.uci.edu › index.php › 79-python-resources › 104-try-except
Try, Except, Else, Finally
This code is executed only if no exceptions were raised in the try block. Code executed in this block is just like normal code: if there is an exception, it will not be automatically caught (and probably stop the program). Notice that if the else block is executed, then the except block is ...
🌐
Nim Forum
forum.nim-lang.org › t › 9034
try except else
Sign up · Log in · Reset your password · Create account · Reset password · Create a new account
🌐
Note.nkmk.me
note.nkmk.me › home › python
Try, except, else, finally in Python (Exception handling) | note.nkmk.me
August 15, 2023 - Basic exception handling in Python: try ... except ... ... Execute action if no exception occurs: try ... except ... else ...
🌐
CodingNomads
codingnomads.com › python-try-except-else
Python: Try Except Else
The try except blocks in Python have an optional else statement. You can use it after any except statements you wrote. The code inside the else block will only execute if the code wrapped in your try statement doesn't raise an exception: try: ...
Top answer
1 of 12
1242
try:
    doSomething()
except Exception: 
    pass

or

try:
    doSomething()
except: 
    pass

The difference is that the second one will also catch KeyboardInterrupt, SystemExit and stuff like that, which are derived directly from BaseException, not Exception.

See documentation for details:

  • try statement
  • exceptions

However, it is generally bad practice to catch every error - see Why is "except: pass" a bad programming practice?

2 of 12
167

It's generally considered best-practice to only catch the errors you are interested in. In the case of shutil.rmtree it's probably OSError:

>>> shutil.rmtree("/fake/dir")
Traceback (most recent call last):
    [...]
OSError: [Errno 2] No such file or directory: '/fake/dir'

If you want to silently ignore that error, you would do:

try:
    shutil.rmtree(path)
except OSError:
    pass

Why? Say you (somehow) accidently pass the function an integer instead of a string, like:

shutil.rmtree(2)

It will give the error "TypeError: coercing to Unicode: need string or buffer, int found" - you probably don't want to ignore that, which can be difficult to debug.

If you definitely want to ignore all errors, catch Exception rather than a bare except: statement. Again, why?

Not specifying an exception catches every exception, including the SystemExit exception which for example sys.exit() uses:

>>> try:
...     sys.exit(1)
... except:
...     pass
... 
>>>

Compare this to the following, which correctly exits:

>>> try:
...     sys.exit(1)
... except Exception:
...     pass
... 
shell:~$ 

If you want to write ever better behaving code, the OSError exception can represent various errors, but in the example above we only want to ignore Errno 2, so we could be even more specific:

import errno

try:
    shutil.rmtree(path)
except OSError as e:
    if e.errno != errno.ENOENT:
        # ignore "No such file or directory", but re-raise other errors
        raise
🌐
Medium
medium.com › @luqmanilman › what-is-the-difference-between-a-try-except-statement-and-an-if-else-statement-in-the-python-92bd4e978dcc
“What is the difference between a ‘try-except’ statement and an ‘if-else’ statement in the Python language?” | by Luqman Ilman Muhammad | Medium
June 7, 2024 - In contrast, “if-else” statements are more general-purpose and are used for controlling program flow based on Boolean conditions, but they do not provide mechanisms for handling exceptions. ... Use “try-except” when you expect certain operations to potentially raise exceptions and want to handle them gracefully without crashing your program.
🌐
GeeksforGeeks
geeksforgeeks.org › python-try-except
Python Try Except - GeeksforGeeks
March 19, 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.
🌐
Delft Stack
delftstack.com › home › howto › python › try without except in python
How to try Without except in Python | Delft Stack
March 11, 2025 - This method allows you to enforce certain conditions in your code without using the traditional try and except structure. In summary, while the try and except blocks are essential for handling exceptions in Python, there are several ways to use try without an except clause.