[Python-Dev] Exception masking/chaining

Guido van Rossum guido@python.org
Thu, 12 Jun 2003 10:56:33 -0400


> So maybe the plan should look somethink like this:
> 
> 1. Deprecate string exceptions.
> 
> 2. Make Exception a new-style class.
> 
> 3. Require exceptions to be derived from Exception.
> 
> 4. Make the traceback an attribute of the exception object.
> 
> 5. Implement exception chaining (as an attribute named cause on
>     the outer exception referencing the inner exception).
> 
> 6. Add other exception attributes (e.g. KeyError has an attribute
>     key, IndexError has an attribute index, IOError has attributes
>     filename, errorcode etc.)

I think (1) and (3) aren't absolutely necessary -- strings and classic
classes would simply not have the traceback attribute.  For backwards
compatibility it could still be kept separate in (the per-thread
equivalent of) sys.exc_traceback, but at some point, that would be
phased out and traceback reporting for deprecated exception types
would be reduced -- a nice incentive for moving to new-style
exceptions!  (Or, for classic classes, it wouldn't be okay to store
the traceback on a system attribute, e.g. __traceback__.)

--Guido van Rossum (home page: http://www.python.org/~guido/)