[Python-Dev] Exception masking/chaining

Brett C. drifty@alum.berkeley.edu
Tue, 10 Jun 2003 13:36:06 -0700

Walter D=F6rwald wrote:
> 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 outermos=
> exception,

That's what I was thinking.  Simpler and no surprises for old code.

> 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=3D=3D42:
>          raise TypeError
>       return None
> x =3D filter(None, spam())
> this might give:
> Traceback (most recent call last):
>   File "spam.py", line 8, in ?
>     x =3D filter(None, foo())
>   File "spam.py", line 4, in __getitem__
>     raise TypeError("ouch")
> TypeError: ouch
> Info: in filter() at index 42

So have a function that takes the current exception, stores it as an=20
attribute of a new one, and have exception-handling code no of the=20
possible existence of the attribute and print out the info if it exists?