16
votes

From within an __exit__ block in a custom cursor class I want to catch an exception so I can in turn throw a more specific exception. What is the proper way to do this?

class Cursor:
    def __enter__(self):
        ...

    def __exit__(self, ex_type, ex_val, tb):
        if ex_type == VagueThirdPartyError:
            # get new more specific error based on error code in ex_val and
            # return that one in its place.
            return False # ?
        else:
            return False

Raising the specific exception within the __exit__ block seems like a hack, but maybe I'm over thinking it.

1
Why does it seem like a hack to just raise the exception? - NPE
I thought there might have been another way to do it since the context manager catches any exceptions within the main block, stores it, and then passes it through to exit to approve or veto. Raising within exit seemed like it would be more of an expansion of its responsibilities based on its arguments - but I was clearly over thinking it. - jpredham

1 Answers

25
votes

The proper procedure is to raise the new exception inside of the __exit__ handler.

You should not raise the exception that was passed in though; to allow for context manager chaining, in that case you should just return a falsey value from the handler. Raising your own exceptions is however perfectly fine.

Note that it is better to use the identity test is to verify the type of the passed-in exception:

def __exit__(self, ex_type, ex_val, tb):
    if ex_type is VagueThirdPartyError:
        if ex_val.args[0] == 'foobar':
            raise SpecificException('Foobarred!')

        # Not raising a new exception, but surpressing the current one:
        if ex_val.args[0] == 'eggs-and-ham':
            # ignore this exception
            return True

        if ex_val.args[0] == 'baz':
            # re-raise this exception
            return False

    # No else required, the function exits and `None` is  returned

You could also use issubclass(ex_type, VagueThirdPartyError) to allow for subclasses of the specific exception.