[Python-Dev] Chained Exceptions

Brett C. bac at OCF.Berkeley.EDU
Fri May 13 01:47:56 CEST 2005


Guido van Rossum wrote:
> [Ka-Ping Yee]
> 
>>Suppose exceptions have an optional "context" attribute, which is
>>set when the exception is raised in the context of handling another
>>exception.  Thus:
>>
>>    def a():
>>        try:
>>            raise AError
>>        except:
>>            raise BError
>>
>>yields an exception which is an instance of BError.  This instance
>>would have as its "context" attribute an instance of AError.
> 
> [...]
> 
> I like the idea, but I'm not sure about the consequences, and I'm not
> sure how it can be defined rigorously. Would it only happen when
> something *in* an except clause raises an exception? Which piece of
> code would be responsible for doing this?
> 
> Try to come up with a precise specification and we'll talk.
> 

If a new exception is raised (e.g., not a bare 'raise') while a current
exception is active (e.g., sys.exc_info() would return something other than a
tuple of None), then the new exception is made the active exception and the now
old exception is assigned to the new exception's context attribute to be the
old exception.

-Brett


More information about the Python-Dev mailing list