Usually, this is a bad practice. But here you go:

try:
    <your code here>
except:
   pass

This will try your code and if it fails then does nothing.

Example:

In Python, pass is a null statement. The interpreter does not ignore a pass statement, but nothing happens and the statement results in no operation. The pass statement is useful when you don't write the implementation of a function but you want to implement it in the future.

Answer from Jacob Celestine 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 - 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: ... If I understand correctly, I am basically using except for something finally should be used for at the moment, yes?
🌐
Python.org
discuss.python.org › python help
[SOLVED]How to catch error signals without try / except - Python Help - Discussions on Python.org
December 18, 2021 - Hi, First time posting here. I’d like to catch any error signal produced, be it a NameError, IndentationError, or whatnot. I don’t care to escape them, I would just like to be able to call some simple function whenever such an error occurs. Some people might say that this is a bad idea, ...
Discussions

python - How to properly ignore exceptions - Stack Overflow
When you just want to do a try-except without handling the exception, how do you do it in Python? Is the following the right way to do it? try: shutil.rmtree(path) except: pass More on stackoverflow.com
🌐 stackoverflow.com
python - What is more Pythonic way to handle try-except errors? - Software Engineering Stack Exchange
Neither of these is more Pythonic than the other. The examples are too trivial to say which is preferrable but it really all depends on how things should work. Catch and logging/reporting an issue is just a hair's breadth away from squashing exceptions which is almost always a terrible idea. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
November 1, 2023
PEP 760 – No More Bare Excepts - PEPs - Discussions on Python.org
‼Update: We have decided to withdraw the PEP as the consensus is that the benefits doesn’t justify the cost ———- 👋 Hi everyone, As the evil twin of PEP 758: Allow `except` and `except*` expressions without parentheses, @brettcannon and me present you PEP 760: No more bare excepts. More on discuss.python.org
🌐 discuss.python.org
6
October 9, 2024
language agnostic - Why use try … finally without a catch clause? - Software Engineering Stack Exchange
Whether this is good or bad is ... to exception handling. ... @yfeldblum has the correct answer: try-finally without a catch statement should usually be replaced with an appropriate language construct. In C++, it's using RAII and constructors/destructors; in Python it's a with ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
These exceptions can be handled ... an error, the except block will be executed. Without the try block, the program will crash and raise an error:...
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.3 documentation
The except clause may specify a ... args attribute that stores the arguments. For convenience, builtin exception types define __str__() to print all the arguments without explicitly accessing .args. >>> try: ......
Top answer
1 of 12
1240
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
166

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
Find elsewhere
🌐
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 ...
🌐
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 3
5

Neither of these is more Pythonic than the other. The examples are too trivial to say which is preferrable but it really all depends on how things should work.

Catch and logging/reporting an issue is just a hair's breadth away from squashing exceptions which is almost always a terrible idea. The only reason I can see doing this is that you want whatever the issue is to not stop execution. If you are going to do something like this, it's really crucial to make sure that you return something sensible that works for the caller. If the next thing that happens is that the calling code throws its own exception because e.g., None doesn't have an add method, you are at best just making things harder to troubleshoot. It could be a lot worse, however. A lot of serious bugs are due to returning nulls/None after catching an error. I think there are times that is makes sense to do this, but they are rare in my experience.

Allowing the raw exception to bubble out is the next least-worst option, IMO. This can be fine if you are building something small where it will be easy to find the what the problem is when things crash with a KeyError. In a situation where you are leveraging a lot of duck-typing, passing around function references, or using annotations, it can sometimes be difficult. For example, if you are using this code behind a web endpoint, what HTTP error code should you use when you catch a KeyError. 500 might be the right answer in most cases but there might be times you want to produce something else depending on where the key was not found.

That brings me to the last option which you don't mention: catch and raise a separate, more meaningful error. That allows you to distinguish between say, a KeyError thrown because the request was for something that isn't valid and a KeyError thrown because of a bad configuration.

2 of 3
4

Neither is pythonic. Pythonic code would be:

my_dict = {}
def fetch_value(key):
    return my_dict[key]

val = fetch_value('my_key')

Remember, simple is better than complex and flat is better than nested. Since your except-block does not handle the exception in any meaningful way, it is better to just let it bubble up the call stack and terminate the program.

But in your code the error is ignored and it implicitly returns None if the key is not found. If this is what you want, then it can be done simpler with the get() method:

def fetch_value(key):
    return my_dict.get(key)

"Handling" an error by just logging a message and then continuing as if nothing happened, is a terrible antipattern from the Java world which has no place in Python. Exceptions should only be caught if they can be meaningfully handled.

🌐
Quora
quora.com › How-do-you-ignore-an-exception-and-proceed-in-python
How to ignore an exception and proceed in python - Quora
Answer (1 of 2): First off, let’s be clear that ignoring exceptions is often not the right thing to do. The classical way to do it is to just accept the exception and pass: [code]>>> def remover(filename): ... import os ... try: ... os.remove(filename) ... except FileNotFoun...
🌐
W3Resource
w3resource.com › python-interview › what-is-the-use-of-the-except-statement-without-specifying-an-exception-type.php
Using the except statement without Exception Type in Python
This is often called a "catch-all" or "catch-everything" exception handler. When we use "except" without specifying an exception type, it will accept any exception that occurs within the corresponding "try" block.
🌐
Python.org
discuss.python.org › peps
PEP 760 – No More Bare Excepts - PEPs - Discussions on Python.org
October 9, 2024 - ‼Update: We have decided to withdraw the PEP as the consensus is that the benefits doesn’t justify the cost ———- 👋 Hi everyone, As the evil twin of PEP 758: Allow `except` and `except*` expressions without parentheses, @brettcannon and me present you PEP 760: No more bare excepts.
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
December 1, 2024 - What does try … except pass do in Python?Show/Hide · Using try … except with pass allows the program to ignore the exception and continue execution without taking any specific action in response to the error.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-try-except
Python Try Except - GeeksforGeeks
July 23, 2025 - In Python, errors and exceptions can interrupt the execution of program. Python provides try and except blocks to handle situations like this. In case an error occurs in try-block, Python stops executing try block and jumps to exception block. These blocks let you handle the errors without crashing ...
🌐
Plain English
python.plainenglish.io › stop-using-try-except-like-a-trash-can-c25934344f77
Stop Misusing Python Try/Except for Errors | Python in Plain English
June 10, 2025 - To be clear, this is not handling errors. Error concealing is what this is. You’re ignoring exceptions and hoping they won’t resurface on a production server at two in the morning. ... New Python content every day.
🌐
freeCodeCamp
freecodecamp.org › news › error-handling-in-python-introduction
Error Handling in Python – try, except, else, & finally Explained with Code Examples
April 11, 2024 - Are there any considerable impacts to code performance, though? 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.
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.

🌐
Bobby Hadz
bobbyhadz.com › blog › python-try-without-except
Using try without except (ignoring exceptions) in Python | bobbyhadz
The pass statement does nothing and is used when a statement is required syntactically but the program requires no action. In general, using an except statement without explicitly specifying the exception type is considered a bad practice.
🌐
Rollbar
rollbar.com › home › when to use try-except vs. try-catch
When to Use Try-Except vs. Try-Catch | Rollbar
July 31, 2023 - Yet only in Python can you have code that runs if no exception occurs. The else and finally blocks in both Python and Java are optional. Let’s take a look at several examples. try: print(name) except NameError: print("Variable name is not defined") except: print("Something went wrong")
🌐
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 - If you call a Python function inside the try block, and an exception occurs in that function, the flow of code execution stops at the point of the exception and the code in the except block is executed. Try doing this again without try and except. You’ll see that Python prints the exception for us.