I have several context managers for a project I'm working on. It is about to ship and I've run into something I'm starting to panic about.
I was under the impression that you should not reraise the exceptions passed as arguments to the __exit__ method of the context manager class. However, I was preforming some testing and it seemed like a context manager was suppressing an exception that was being thrown inside it. When I changed my __exit__ methods to look like this:
def __exit__(self, type_, value, trace):
if trace is not None:
print('ERROR IN TRACEBACK: ' + str(value))
# PYTHON 2.7 RAISE SYNTAX:
raise type_, value, trace
the errors seemed to pass through correctly.
My question is: What is the correct way to deal with exceptions in the __exit__ method if type_, value, and trace are not None? Is it bad to raise (reraise?) the exception like this? Is this how I'm supposed to do it?
The error I encountered might have been caused by something else. Normally I would go through and test this all thoroughly, but my time seems to be very limited at the moment. I'm hoping somebody can explain the proper implementation of this function and
Ultimately: Can I safely leave the raise type_, value, trace in my context manager __exit__ methods?