SyntaxStudy
Sign Up
Python Intermediate 3 min read

Suppressing Exceptions

Suppressing Exceptions

contextlib.suppress is a clean way to ignore specific exceptions without a try/except block.

Example
from contextlib import suppress

with suppress(FileNotFoundError):
    import os
    os.remove("temp.txt")
print("Continued regardless")
Pro Tip

Use suppress when you genuinely want to ignore an exception silently.