[Python-Dev] exception chaining

Ethan Furman ethan at stoneleaf.us
Fri Jan 20 18:09:57 CET 2012


Summary:

Exception Chaining is cool, unless you are writing libraries that want 
to transform from Exception X to Exception Y as the the previous 
exception context is unnecessary, potentially confusing, and cluttery 
(yup, just made that word up!).

For all the gory details, see http://bugs.python.org/issue6210.

I'm going to attempt a patch implementing MRAB's suggestion:

try:
     some_op
except ValueError:
     raise as OtherError() # `raise` keeps context, `raise as` does not

The question I have at the moment is:  should `raise as` be an error if 
no exception is currently being handled?

Example:

def smurfy(x):
     if x != 'magic flute':
          raise as WrongInstrument
     do_something_with_x

If this is allowed then `smurfy` could be called from inside an `except` 
clause or outside it.

I don't care for it for two reasons:

   - I don't like the way it looks
   - I can see it encouraging always using `raise as` instead of `raise` 
and losing the value of exception chaining.

Other thoughts?

~Ethan~


More information about the Python-Dev mailing list