try:
    do_something()
except:
    pass

You will use the pass statement.

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

Answer from Andy on Stack Overflow
🌐
CodeQL
codeql.github.com › codeql-query-help › python › py-empty-except
Empty except — CodeQL query help documentation
The loss of information can lead to hard to debug errors and incomplete log files. It is even possible that ignoring an exception can cause a security vulnerability. An empty except block may be an indication that the programmer intended to handle the exception, but never wrote the code to do so.
Discussions

Is it OK to catch an exception and do nothing, if the exception is expected?
Sure. try: do_something() except SomeError: pass is common enough that contextlib.suppress is in the standard library: with contextlib.suppress(SomeError): do_something() More on reddit.com
🌐 r/learnpython
67
106
May 29, 2019
How to catch empty user input using a try and except in python? - Stack Overflow
I am trying to figure out how I can catch empty user input using a try and except. If you had this for example: try: #user input here. integer input except ValueError: #print statement sa... More on stackoverflow.com
🌐 stackoverflow.com
Is it okay to leave except EMPTY, when I print out trackback anyway? [Python 3.10] - Stack Overflow
I understand that it's usually not a good idea to leave "except" empty as errors will go unnoticed. For example: def test4(): try: 0/0 except: pass print('PROG... More on stackoverflow.com
🌐 stackoverflow.com
exception - empty error message in python - Stack Overflow
I'm trying to debug an error, I got a "no exception supplied" when I ran it initially and then later put in a try/except block to print out whatever the error was. try: #some code except More on stackoverflow.com
🌐 stackoverflow.com
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 381248 › using-try-except-to-catch-a-blank-input
python - Using Try, Except to catch a blank input [SOLVED] | DaniWeb
In python 3, after result = input(prompt) , result is a string in the python sense (an instance of the datatype 'str'). Examples of strings are · "" # the empty string " " # a string of white space "3.14159" # a string with the representation … — Gribouillis 1,391 Jump to Post · You can pass the acceptable values to the checkInput() function and let it do the work. For the getFloat() function, use a try/except to test for correct input.
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.4 documentation
The base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() is called on an instance of this class, the representation of the argument(s) to the instance are ...
🌐
Java2s
java2s.com › example › python-book › catching-all-the-empty-except-and-exception.html
Python - Catching all: The empty except and Exception
Python · Exception · try/except/else Statement · To have a general "catchall" clause, an empty except does the trick: try: action() except NameError: ... # Handle NameError except IndexError: ... # Handle IndexError except: ... # Handle all other exceptions else: ...
🌐
Derscanner
derscanner.com › vulnerability-database › Python-:-Error-handling:-empty-except-block
DerScanner Vulnerability Database: Python : Error handling: empty except block
The application contains an empty catch block, i.e. it catches an exception but does not handle it. Exception catch without its handling makes it more difficult to diagnose an error and correct it. Ignoring exceptions and other error conditions may allow an attacker to induce unexpected behavior unnoticed. CWE-391: Unchecked Error Condition Errors and Exceptions - docs.python.org
🌐
Python
peps.python.org › pep-0760
PEP 760 – No More Bare Excepts | peps.python.org
October 2, 2024 - The syntax for the except clause will be modified to require an exception type. The grammar will be updated to remove the possibility of adding an empty expression in except clauses.
Find elsewhere
🌐
Astral
docs.astral.sh › ruff › rules › except-with-empty-tuple
except-with-empty-tuple (B029) | Ruff - Astral Docs
An exception handler that catches an empty tuple will not catch anything, and is indicative of a mistake. Instead, add exceptions to the except clause. try: 1 / 0 except (): ... Use instead: try: 1 / 0 except ZeroDivisionError: ... Python documentation: except clause Back to top
🌐
Reddit
reddit.com › r/learnpython › is it ok to catch an exception and do nothing, if the exception is expected?
r/learnpython on Reddit: Is it OK to catch an exception and do nothing, if the exception is expected?
May 29, 2019 -

I'm struggling to word this properly but here goes anyway.

If we know a specific exception will be thrown under certain circumstances, is it OK to catch that exception and not tell the user or log it anywhere? Or should an exception always be logged somewhere?

For example, let's say KeyError is expected under certain circumstances, e.g. if a dictionary that is created programmatically doesn't contain an item, yet. In one of my apps, I have that exact situation because the item that will eventually reside there hasn't been extracted from my XML file, yet. Until that time, I'm catching the KeyError, and carrying on knowing the KeyError eventually won't be thrown when I finish processing the XML. Terrible explanation, but I hope it makes sense.

Is that OK or would it be better practice to initialise the dictionary first, say in the class __init__ method so that the KeyError never happens, ever?

Thanks

🌐
Appmarq
appmarq.com › public › security,1021018,Avoid-catch-all-except-blocks-with-empty-handlers
Avoid catch-all except blocks with empty handlers
>>> # easy remediation >>> try: >>> doSomething() >>> except: >>> logging.debug("Someting happened") >>> # better remediation >>> try: >>> doSomething() >>> except SomeException as e: >>> logging.debug("Something happened:" + e.error) >>> except: >>> logging.debug("Something unexpected happened ...")
🌐
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 an except clause mentions a particular class, that clause also handles any exception classes derived from that class. An empty except is equivalent to except BaseException, hence it will catch all possible exceptions.
🌐
O'Reilly
oreilly.com › library › view › clean-code-in › 9781788835831 › 32f17e39-fd0b-4081-a9d1-25fa519e2731.xhtml
Avoid empty except blocks - Clean Code in Python [Book]
August 29, 2018 - This was even referred to as the most diabolical Python anti-pattern (REAL 01). While it is good to anticipate and defend our programs against some errors, being too defensive might lead to even worse problems. In particular, the only problem with being too defensive is that there is an empty except block that silently passes without doing anything.
Author   Mariano Anaya
Published   2018
Pages   332
🌐
Firas
firas.io › posts › python exception handling
Python Exception Handling | Firas Sadiyah
February 25, 2024 - We can use a try-except block to wrap the code that may raise the exception. If the exception occurs, we’ll print the message, return the empty dataframe without any further processing.
🌐
Iditect
iditect.com › faq › python › tryexcept-clause-with-an-empty-except-code-in-python.html
Try-except clause with an empty except code in python
How to safely use a general except block in Python? If you use a general except, ensure you handle the exception appropriately, like logging or re-raising it.
🌐
Stack Overflow
stackoverflow.com › questions › 72298793 › is-it-okay-to-leave-except-empty-when-i-print-out-trackback-anyway-python-3-1
Is it okay to leave except EMPTY, when I print out trackback anyway? [Python 3.10] - Stack Overflow
I understand that it's usually not a good idea to leave "except" empty as errors will go unnoticed. For example: def test4(): try: 0/0 except: pass print('PROG...
🌐
McNeel Forum
discourse.mcneel.com › rhino developer
Python Script Try Blocks / Error Handling - Rhino Developer - McNeel Forum
June 17, 2022 - Is there a way to use try / except / finally blocks in Python Script? Is there documentation somewhere on error handling?