Sure. try: do_something() except SomeError: pass is common enough that contextlib.suppress is in the standard library: with contextlib.suppress(SomeError): do_something() Answer from _lilell_ on reddit.com
🌐
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

Discussions

Can someone please explain the pass statement?
I’m just wondering what the purpose of the pass statement is and how its used. The answers I’ve found are vague saying it does nothing- so what is it used for? Any examples? Thanks! Dave More on discuss.python.org
🌐 discuss.python.org
8
1
January 26, 2023
Type, except, not working
However, if your indentation is correct your except execute. As a side note - I don’t understand what this code should accomplish · Function names should be lowercase, with words separated by underscores as necessary to improve readability · They look ok to me. More on discuss.python.org
🌐 discuss.python.org
5
0
June 5, 2021
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
Is common best practice in python to use assert for business logic?
Don’t use assert outside of tests. the Python runtime has a flag -O (for optimize) that ignores assert statements. If you use asserts for business logic, and someone decides to run your code in production and thinks it’s a good idea to optimize the bytecode, your code breaks More on reddit.com
🌐 r/Python
138
205
July 14, 2024
🌐
Real Python
realpython.com › python-pass
The pass Statement: How to Do Nothing in Python – Real Python
September 25, 2023 - In that situation, you can use the pass statement to silence the error. If you want to make sure a file doesn’t exist, then you can use os.remove(). This function will raise an error if the file isn’t there.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-ignore-an-exception-and-proceed-in-python
How to Ignore an Exception and Proceed in Python - GeeksforGeeks
July 23, 2025 - To ignore an exception and continue execution in Python using suppress, you can use the with a statement with the suppress function from the contextlib module.
🌐
Quora
quora.com › How-do-you-ignore-an-exception-and-proceed-in-python
How do you ignore an exception and proceed in python?
Use a try/except that catches the exception and does nothing (or logs) in the except block.
🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
You can use the else keyword to define a block of code to be executed if no errors were raised: In this example, the try block does not generate any error: try: print("Hello") except: print("Something went wrong") else: print("Nothing ...
🌐
Emmanuel Tanimowo's
mannuel.hashnode.dev › handling-exceptions-in-python
Handling Exceptions In Python
December 5, 2021 - When an exception occurs, you may ... construct a try block as usual, but in the except block, use the pass keyword to specifically tell Python to do nothing, as seen below....
Find elsewhere
🌐
PythonHello
pythonhello.com › problems › exceptions › how-to-properly-ignore-exceptions
How to properly Ignore Exceptions in Python
try: # some code that might raise an exception x = 1 / 0 except ZeroDivisionError: # this block of code will be executed if a ZeroDivisionError is raised # we can ignore the exception by doing nothing here pass
🌐
Python.org
discuss.python.org › python help
Can someone please explain the pass statement? - Python Help - Discussions on Python.org
January 26, 2023 - I’m just wondering what the purpose of the pass statement is and how its used. The answers I’ve found are vague saying it does nothing- so what is it used for? Any examples? Thanks! Dave
🌐
Python.org
discuss.python.org › python help
Type, except, not working - Python Help - Discussions on Python.org
June 5, 2021 - why my excepts are not executing? def TestNumbers(x,y,z): try: X = int(x) y = int(y) z = int(z) print(‘You have provided valid values’) except NameError: print('NameError') except: print("You have provided invalid inputs to the function") if x == y and x == z: return True else: return False print(TestNumbers(5,6,8)) print() print(TestNumbers(“a”,5,8)) print(TestNumbers(a,6,7))
🌐
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.
🌐
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 ...
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Exceptions › intro-exceptions.html
19.1. What is an exception? — Foundations of Python Programming
If we catch only IndexEror, and we actually have a divide by zero error, the program does stop executing. There’s one other useful feature. The exception code can access a variable that contains information about exactly what the error was. Thus, for example, in the except clause you could print out the information that would normally be printed as an error message but continue on with execution of the rest of the program.
🌐
Inductive Automation
docs.inductiveautomation.com › ignition platform › scripting › python scripting › error handling
Error Handling | Ignition User Manual
September 27, 2024 - Within Python, we can use the try and except blocks to handle errors. We would first use try: and write the code we would like to try indented underneath it. We then must have an except: with code that will run if there is an error. ... # With try, we can attempt any amount of code try: some ...
🌐
Built In
builtin.com › software-engineering-perspectives › pass-vs-continue-python
Pass vs. Continue in Python Explained | Built In
This explains how the break statement is used to stop the process. Pass in Python is used as a placeholder for future code in statement blocks and loops. Nothing happens when a pass statement is executed, but it can fill statement spaces in ...
🌐
Dummies
dummies.com › article › technology › programming-web-design › python › how-to-raise-exceptions-in-python-148326
How to Raise Exceptions in Python | dummies
July 3, 2025 - Notice that this try … except ... there is nothing to do after the call. Although you rarely use a try … except block in this manner, you can. You may encounter situations like this one sometimes and need to remember that adding the else clause is purely optional. On the other hand, you must add at least one except clause. Click Run Cell. Python displays the ...
🌐
Learn By Example
learnbyexample.org › python-exceptions-try-except
Python Exceptions (Try...Except) - Learn By Example
April 20, 2020 - try: x = 1/1 except: print('Something went wrong') else: print('Nothing went wrong') # Prints Nothing went wrong
🌐
Jerry Ng
jerrynsh.com › stop-using-exceptions-like-this-in-python
Stop Using Exceptions Like This in Python
January 19, 2024 - Secondly, we should avoid raising a generic Exception in Python because it tends to hide bugs. Here's another example that you should avoid using: # Do NOT raise a generic Exception: def get_book_List(): try: if not fetch_books(): raise Exception("This exception will not be caught by specific catch") # !!!
🌐
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.