Implement a Context Manager That Suppresses Exceptions in Python

Shows how to write a custom context manager that catches specified exceptions and optionally re-raises others, plus the stdlib contextlib.suppress alternative.

Easy Python 3.9+ Aug 9, 2026 Errors & debugging 12 views 0 copies

Python code

31 lines
Python 3.9+
import contextlib

class SuppressExceptions:
    def __init__(self, *exceptions):
        self.exceptions = exceptions

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is None:
            return False
        if not self.exceptions or exc_type in self.exceptions:
            print(f"Suppressed: {exc_type.__name__}: {exc_val}")
            return True
        return False


if __name__ == "__main__":
    with SuppressExceptions(ValueError, TypeError):
        result = int("not_a_number")
        print("This line won't execute")

    with SuppressExceptions(ValueError):
        raise TypeError("This won't be suppressed")

    with contextlib.suppress(ZeroDivisionError):
        x = 1 / 0
        print("This line won't execute either")

    print("Program completed successfully")

Output

stdout
Suppressed: ValueError: invalid literal for int() with base 10: 'not_a_number'
Program completed successfully

How it works

The __exit__ method receives the exception type, value, and traceback when an exception occurs inside the with block. Returning True tells Python to suppress the exception; returning False or None lets it propagate. The custom class checks whether the raised exception type is in its allowlist and only suppresses those. The standard library's contextlib.suppress provides the same behavior with less code, making it ideal for simple cases. Both approaches ensure the rest of the program continues after a suppressed exception.

Common mistakes

  • Forgetting to return `True` from `__exit__` to actually suppress the exception
  • Not handling `exc_type is None` (no exception raised) and returning `False`
  • Checking subclasses instead of exact types — the `in` check won't match subclasses
  • Using a bare `except` with `contextlib.suppress()` which suppresses everything unintentionally

Variations

  1. Use `contextlib.suppress(ValueError, TypeError)` for a one-liner suppression.
  2. Use a decorator like `@contextmanager` to write a generator-based context manager.

Real-world use cases

  • Wrapping legacy code that raises expected exceptions, logging them but continuing the application flow.
  • Ignoring specific failures during cleanup tasks, like deleting temporary files that may already be gone.
  • Suppressing non-critical errors in batch processing loops so one bad record doesn't abort the whole job.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Errors & debugging

Related tutorials and quizzes for this topic.