[Python-Dev] Exception masking/chaining

Walter Dörwald walter@livinglogic.de
Tue, 10 Jun 2003 21:09:44 +0200


Brett C. wrote:
> Brett C. wrote:
> 
>> Greg Ewing wrote:
>>
>>>> can we now consider implementing chained exceptions as has been 
>>>> mentioned here numerous times?
>>>
>>>
>>>
>>>
>>> I must have missed that. What are chained exceptions?
>>>
> <SNIP>
> 
>> This was discussed on the list between Ping and I think Walter (could 
>> be wrong about Walter but I know Ping was involved).  Should be in the 
>> Summary archive somewhere; just need to find it.  =)
>>
> 
> Found it.
> 
> Raymond apparently instigated the thread: 
> http://mail.python.org/pipermail/python-dev/2003-January/032492.html
> 
> And it continued into the next month: 
> http://mail.python.org/pipermail/python-dev/2003-February/032864.html
> 
> And you even contributed to the discussion, Greg =) :
> http://mail.python.org/pipermail/python-dev/2003-January/032513.html
> 
> But the original terminology was "exception masking" so that may have 
> been why this didn't ring any bells.
> 
> For the impatient you can read the last 3 emails I think (especially the 
> absolute last which was authored by Raymond) to get a summary of the 
> ideas proposed.

An open question for exception chaining was which exception type is
tested in the except statement, the innermost or the outermost one.

I'd say for backwards compatibility reasons this should be the outermost
exception, but we could add a new feature to that: Add a new exception
subclass named Info that can be used to wrap exceptions without
influencing the type of the exception raised. This could be used
by code higher up in the call chain to add information to the
exception without changing the type that gets caught in the except
statement. For example filter() could be changed to add information
about the iteration index where the exception happened:

class spam:
    def __getitem__(self, index):
       if index==42:
          raise TypeError
       return None

x = filter(None, spam())

this might give:

Traceback (most recent call last):
   File "spam.py", line 8, in ?
     x = filter(None, foo())
   File "spam.py", line 4, in __getitem__
     raise TypeError("ouch")
TypeError: ouch
Info: in filter() at index 42

Bye,
    Walter Dörwald