Python
Exception Handling
Programming
Error Management
Code Troubleshooting

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.

Browse interview questions

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.

python
1try:
2    value = int("not-a-number")
3except ValueError:
4    pass
5
6print("program continues")

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.

python
1for item in ["1", "x", "3"]:
2    try:
3        print(int(item))
4    except ValueError:
5        continue

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.

python
1import logging
2
3logging.basicConfig(level=logging.INFO)
4
5try:
6    open("missing.txt")
7except FileNotFoundError as exc:
8    logging.info("Optional file missing: %s", exc)
9
10print("still running")

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.”

python
1from contextlib import suppress
2
3with suppress(FileNotFoundError):
4    open("optional.txt")
5
6print("continued")

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 AttributeError from 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.

python
1results = []
2for raw in ["10", "bad", "20"]:
3    try:
4        results.append(int(raw))
5    except ValueError:
6        continue
7
8print(results)

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, or contextlib.suppress only 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.