
31.03.20 01:32, Christopher Barker пише:
In case Serhiy's answer wasn't clear: context managers can be written to handle exceptions (within their context) in any way you see fit.
that is: the method: | | |__exit__(||self||, exc_type, exc_value, exc_traceback):|
get the exception, and information about it, of one is raised, so you can handle it anyway you want.
Actually I meant the opposite. Sorry for being unclear. In normal case the context manager does not silence a raised exception, so control flow is never passed to the statement past the with block if an exception is raised inside the with block. But if you use a context manager which silences the exception, like contextlib.suppress() or unittest.TestCase.assertRaises(), it is easy to do too. was_not_raised = False with my_context(): do_something_sensitive() was_not_raised = True if was_not_raised: print("We're all safe.") You do not need a special syntax for this. was_not_raised will never be set to True if do_something_sensitive() raises an exception.