Python How to ignore an exception and proceed?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Ignoring an exception in Python means catching it and continuing execution instead of letting it terminate the current flow. That is easy to write, but it is only safe when the exception is expected, well understood, and genuinely non-fatal.
The best practice is not “ignore exceptions.” The best practice is “catch only the specific exceptions you are willing to tolerate, and make that choice explicit.”
The Basic Pattern
To catch an exception and continue, use try and except.
This works, but pass should not become your default response to errors. You should know why the exception is safe to ignore.
Catch Specific Exceptions Only
Always prefer a specific exception type over a blanket except Exception: unless you have a very strong reason.
This ignores only invalid integer conversions and continues with the next item.
That is much safer than swallowing every possible error from the block.
Logging Is Often Better Than Silent Ignore
If the exception is expected but still potentially useful for diagnosis, log it before continuing.
This preserves observability while keeping the program flow alive.
contextlib.suppress for Small, Intentional Cases
Python provides a very readable helper for “ignore exactly this exception type.”
This is especially nice when the exception tolerance is local, simple, and obvious.
When Continuing Is the Wrong Choice
Not every exception should be ignored. If the error means your program state is invalid, continuing may only make the failure harder to understand later.
Examples where silent ignore is risky:
- database write failures
- authentication failures
- corrupted input that invalidates later logic
- programming bugs such as
AttributeErrorfrom bad assumptions
Continuing is only correct when the program still has a valid next step.
Looping Workloads Are a Common Use Case
Ignoring and proceeding is most legitimate in per-item processing loops where one bad item should not stop the whole batch.
This keeps the good records and skips only the bad one.
Do Not Hide Programming Mistakes
Catching broad exceptions can accidentally hide real bugs. For example, if you meant to catch ValueError but instead catch everything, a typo or attribute bug may be silently swallowed and the program will appear to “work” incorrectly.
That makes debugging much harder than letting the program fail loudly.
Common Pitfalls
A common mistake is using bare except: or except Exception: when only one specific error was expected.
Another mistake is swallowing an exception without logging, metrics, or any trace, which makes later debugging nearly impossible.
Developers also sometimes continue after an exception even though the program state is now incomplete or invalid.
Finally, pass is not a design. It is just syntax. The real question is whether ignoring the exception is semantically correct for the application.
Summary
- Catch specific exceptions you truly expect and can safely tolerate.
- Use
pass,continue, orcontextlib.suppressonly when ignoring the error is intentional. - Prefer logging expected exceptions rather than silently swallowing them.
- Do not continue after exceptions that leave the program in an invalid state.
- The maintainable rule is not “ignore errors,” but “ignore only the exact errors that are non-fatal by design.”
Related reading
- Python how to mock a kafka topic for unit tests?
- Python How to retrieve the best model from Optuna LightGBM study?
- Python How to type hint tf.keras object in functions?
- python How to use POS part of speech features in scikit learn classfiers SVM etc
- Python Image Library fails with message decoder JPEG not available - PIL
- Python json.loads shows ValueError Extra data
- Python idiom to return first item or None
- Python 'If not' syntax
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.