🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
The finally block lets you execute code, regardless of the result of the try- and except blocks. When an error occurs, or exception as we call it, Python will normally stop and generate an error message.
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.5rc1 documentation
At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same try statement. An except clause may name multiple exceptions, for example: ... except RuntimeError, TypeError, NameError: ... pass
Discussions

Enhanced try() function - Ideas - Discussions on Python.org
Consider this code: try: # potential erroneous statement # other statements dependable on the above statement except: pass Meaning, I have a statement that may cause error and next statements should execute only if no error occurs. However, I don’t need some statements that will execute if ... More on discuss.python.org
🌐 discuss.python.org
0
April 20, 2021
python - Why is "except: pass" a bad programming practice? - Stack Overflow
Moreover, you don't event know, which error exactly occurred, because except: pass will catch any exception. Second, if we try to abstract away from the Zen of Python, and speak in term of just sanity, you should know, that using except:pass leaves you with no knowledge and control in your system. More on stackoverflow.com
🌐 stackoverflow.com
Python best practice: try except pass? - Stack Overflow
Small question, one for me to just learn best practice more than anything. If there is a better way to do this please critique :) Basically I have multiple GPIO inputs that correspond to one button... More on stackoverflow.com
🌐 stackoverflow.com
Python: How to ignore an exception and proceed? - Stack Overflow
What I just want to totally ignore ... write 10 try except pass blocks. 2018-11-02T00:46:36.717Z+00:00 ... Using except Exception instead of a bare except avoid catching exceptions like SystemExit, KeyboardInterrupt etc. Because of the last thrown exception being remembered in Python 2, some of ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › shorthand for try: except: pass
r/learnpython on Reddit: Shorthand for try: except: pass
June 11, 2019 -

I'm looking for a shorthand for this pattern:

try:
    ret['x'] = soup.find('h1', {'class': 'entry-title'}).text
except:
    pass

This try-except is necessary because there are particular elements that don't exist on maybe 1% of the pages being crawled. Otherwise, I could've just simply done this:

return {
    'x': soup.find('h1', {'class': 'entry-title'}).text
}

Now imagine there are 10 of these things, and instead of one clean dict, I'm left with a wall of try: except: blocks taking up 4x the lines.

🌐
DEV Community
dev.to › smac89 › better-way-to-tryexceptpass-in-python-2460
Better way to try...except...pass in Python - DEV Community
June 23, 2022 - The typical 'try, exception, ignore' pattern I see people using in python is this: try: # do something that might raise except (MyError1, MyError2, etc) as err: pass · If you've ever found yourself doing something like that, I'm not here to tell you to stop, instead I'll show you a better way.
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
December 1, 2024 - It’s bad practice to catch all exceptions at once using except Exception or the bare except clause. Combining try, except, and pass allows your program to continue silently without handling the exception.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-try-except
Python Try Except - GeeksforGeeks
July 23, 2025 - If any exception occurs, but the except clause within the code doesn't handle it, it is passed on to the outer try statements. If the exception is left unhandled, then the execution stops. A try statement can have more than one except clause ... Example 1: No exception, so the try clause will run. ... # Python code to illustrate # working of try() def divide(x, y): try: # Floor Division : Gives only Fractional Part as Answer result = x // y print("Yeah !
🌐
Python.org
discuss.python.org › ideas
Enhanced try() function - Ideas - Discussions on Python.org
April 20, 2021 - Consider this code: try: # potential erroneous statement # other statements dependable on the above statement except: pass Meaning, I have a statement that may cause error and next statements should execute only if no error occurs. However, I don’t need some statements that will execute if an error occurs.
Top answer
1 of 16
467

As you correctly guessed, there are two sides to it: Catching any error by specifying no exception type after except, and simply passing it without taking any action.

My explanation is “a bit” longer—so tl;dr it breaks down to this:

  1. Don’t catch any error. Always specify which exceptions you are prepared to recover from and only catch those.
  2. Try to avoid passing in except blocks. Unless explicitly desired, this is usually not a good sign.

But let’s go into detail:

Don’t catch any error

When using a try block, you usually do this because you know that there is a chance of an exception being thrown. As such, you also already have an approximate idea of what can break and what exception can be thrown. In such cases, you catch an exception because you can positively recover from it. That means that you are prepared for the exception and have some alternative plan which you will follow in case of that exception.

For example, when you ask for the user to input a number, you can convert the input using int() which might raise a ValueError. You can easily recover that by simply asking the user to try it again, so catching the ValueError and prompting the user again would be an appropriate plan. A different example would be if you want to read some configuration from a file, and that file happens to not exist. Because it is a configuration file, you might have some default configuration as a fallback, so the file is not exactly necessary. So catching a FileNotFoundError and simply applying the default configuration would be a good plan here. Now in both these cases, we have a very specific exception we expect and have an equally specific plan to recover from it. As such, in each case, we explicitly only except that certain exception.

However, if we were to catch everything, then—in addition to those exceptions we are prepared to recover from—there is also a chance that we get exceptions that we didn’t expect, and which we indeed cannot recover from; or shouldn’t recover from.

Let’s take the configuration file example from above. In case of a missing file, we just applied our default configuration and might decide at a later point to automatically save the configuration (so next time, the file exists). Now imagine we get a IsADirectoryError, or a PermissionError instead. In such cases, we probably do not want to continue; we could still apply our default configuration, but we later won’t be able to save the file. And it’s likely that the user meant to have a custom configuration too, so using the default values is likely not desired. So we would want to tell the user about it immediately, and probably abort the program execution too. But that’s not something we want to do somewhere deep within some small code part; this is something of application-level importance, so it should be handled at the top—so let the exception bubble up.

Another simple example is also mentioned in the Python 2 idioms document. Here, a simple typo exists in the code which causes it to break. Because we are catching every exception, we also catch NameErrors and SyntaxErrors. Both are mistakes that happen to us all while programming and both are mistakes we absolutely don’t want to include when shipping the code. But because we also caught those, we won’t even know that they occurred there and lose any help to debug it correctly.

But there are also more dangerous exceptions which we are unlikely prepared for. For example, SystemError is usually something that happens rarely and which we cannot really plan for; it means there is something more complicated going on, something that likely prevents us from continuing the current task.

In any case, it’s very unlikely that you are prepared for everything in a small-scale part of the code, so that’s really where you should only catch those exceptions you are prepared for. Some people suggest to at least catch Exception as it won’t include things like SystemExit and KeyboardInterrupt which by design are to terminate your application, but I would argue that this is still far too unspecific. There is only one place where I personally accept catching Exception or just any exception, and that is in a single global application-level exception handler which has the single purpose to log any exception we were not prepared for. That way, we can still retain as much information about unexpected exceptions, which we then can use to extend our code to handle those explicitly (if we can recover from them) or—in case of a bug—to create test cases to make sure it won’t happen again. But of course, that only works if we only ever caught those exceptions we were already expecting, so the ones we didn’t expect will naturally bubble up.

Try to avoid passing in except blocks

When explicitly catching a small selection of specific exceptions, there are many situations in which we will be fine by simply doing nothing. In such cases, just having except SomeSpecificException: pass is just fine. Most of the time though, this is not the case as we likely need some code related to the recovery process (as mentioned above). This can be for example something that retries the action again, or to set up a default value instead.

If that’s not the case though, for example, because our code is already structured to repeat until it succeeds, then just passing is good enough. Taking our example from above, we might want to ask the user to enter a number. Because we know that users like to not do what we ask them for, we might just put it into a loop in the first place, so it could look like this:

def askForNumber ():
    while True:
        try:
            return int(input('Please enter a number: '))
        except ValueError:
            pass

Because we keep trying until no exception is thrown, we don’t need to do anything special in the except block, so this is fine. But of course, one might argue that we at least want to show the user some error message to tell him why he has to repeat the input.

In many other cases though, just passing in an except is a sign that we weren’t really prepared for the exception we are catching. Unless those exceptions are simple (like ValueError or TypeError), and the reason why we can pass is obvious, try to avoid just passing. If there’s really nothing to do (and you are absolutely sure about it), then consider adding a comment why that’s the case; otherwise, expand the except block to actually include some recovery code.

except: pass

The worst offender though is the combination of both. This means that we are willingly catching any error although we are absolutely not prepared for it and we also don’t do anything about it. You at least want to log the error and also likely reraise it to still terminate the application (it’s unlikely you can continue like normal after a MemoryError). Just passing though will not only keep the application somewhat alive (depending on where you catch of course), but also throw away all the information, making it impossible to discover the error—which is especially true if you are not the one discovering it.


So the bottom line is: Catch only exceptions you really expect and are prepared to recover from; all others are likely either mistakes you should fix or something you are not prepared for anyway. Passing specific exceptions are fine if you really don’t need to do something about them. In all other cases, it’s just a sign of presumption and being lazy. And you definitely want to fix that.

2 of 16
281

The main problem here is that it ignores all and any error: Out of memory, CPU is burning, user wants to stop, program wants to exit, Jabberwocky is killing users.

This is way too much. In your head, you're thinking "I want to ignore this network error". If something unexpected goes wrong, then your code silently continues and breaks in completely unpredictable ways that no one can debug.

That's why you should limit yourself to ignoring specifically only some errors and let the rest pass.

Find elsewhere
🌐
Astral
docs.astral.sh › ruff › rules › try-except-pass
try-except-pass (S110) | Ruff
try: ... except Exception: pass · Use instead: import logging try: ... except Exception as exc: logging.exception("Exception occurred") lint.flake8-bandit.check-typed-exception · Common Weakness Enumeration: CWE-703 · Python documentation: logging Back to top
🌐
Note.nkmk.me
note.nkmk.me › home › python
Try, except, else, finally in Python (Exception handling) | note.nkmk.me
August 15, 2023 - Clean-up action: try ... except ... finally ... Ignore exceptions: pass · Practical example: Reading image files · Without exception handling · With exception handling · You can also set up debugging assertions with the assert statement. The assert statement in Python ·
🌐
GeeksforGeeks
geeksforgeeks.org › python › try-except-else-and-finally-in-python
Try, Except, else and Finally in Python - GeeksforGeeks
Let’s first understand how the Python try and except works · First try clause is executed i.e. the code between try and except clause. If there is no exception, then only try clause will run, except clause will not get executed. If any exception occurs, the try clause will be skipped and except clause will run. If any exception occurs, but the except clause within the code doesn’t handle it, it is passed ...
Published   July 15, 2025
🌐
Python Basics
pythonbasics.org › home › python basics › try and except in python
Try and Except in Python - pythonbasics.org
class LunchError(Exception): pass raise LunchError("Programmer went to lunch") You made a user-defined exception named LunchError in the above code. You can raise this new exception if an error occurs. Outputs your custom error: $ python3 example.py Traceback (most recent call last): File "example.py", line 5, in ·
🌐
Reddit
reddit.com › r/learnpython › can i pass multiple arguments to a try:except clause and have an except value based on which argument causes the error?
r/learnpython on Reddit: Can I pass multiple arguments to a try:except clause and have an except value based on which argument causes the error?
January 2, 2022 -

Not sure if my title makes sense but basic example:

input = _array #expect a list

if not isinstance(_array,list):
    print('Expected a list')
    return

try:
    title = _array[0]
    val1 = _array[1]
    val2 = _array[2]
except:
    val1 = None #if array[1] doesn't exist
    Val2 = None #if array[2] doesn't exist

Can the above be achieved without having three different try/excepts?

My current solution feels a bit tedious:

....
try:
    val1 = _array[1]
except:
    val1 = None
try:
    val2 = _array[2]
except:
    val2 = None

and so on...
🌐
Devcuriosity
devcuriosity.com › manual › details › python-try-except-finally-continue-break-loops
Python - Try, Except, Finally, Continue, Break in Loop Control Flow with examples
finally - Executes at the end of ... that it will still execute if break or continue was called within the try/except block. pass - Simply does nothing and serves as a placeholder....
🌐
Wikiversity
en.wikiversity.org › wiki › Python_Concepts › Try_Statement
Python Concepts/Try Statement - Wikiversity
1. Python's documentation: "8. Errors and Exceptions" "8.4. The try statement" 2. Python's methods: 3.
🌐
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...
🌐
pythoncodelab
pythoncodelab.com › home › continue vs pass loop control statement in python [ explained ]
Continue vs Pass loop control statement in Python [ Explained ]
January 27, 2025 - We can see that we get error at index 1 and 3 where except block code is executed which has pass statement. The pass statement does nothing as execution jumps to next line i.e. printing number. numbers=[1,0,2,0,4,5,8,6,91,4] for number in numbers: try: output=0/number except Exception as e: pass print(number) Output