A Context Manager that Records Suppressed Errors

easy
Unit 5 · Lists & Context Managers0/3 to milestone

contextlib.suppress swallows exceptions silently. Write a class Collect used as a context manager that suppresses the given exception types but records them:

errors = Collect(ValueError, KeyError)
with errors:
    int("x")
errors.caught  # [ValueError(...)]
  • the same instance can be used in several with blocks and keeps appending
  • exceptions of other types propagate normally
  • bool(errors) is True if anything was caught
#context-managers#exceptions2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
A Context Manager that Records Suppressed Errors · TechGradify