[Python-Dev] Idea for avoiding exception masking

Walter Dörwald walter@livinglogic.de
Wed, 29 Jan 2003 14:40:27 +0100


Barry A. Warsaw wrote:

>>>>>>"KY" == Ka-Ping Yee <ping@zesty.ca> writes:
> 
> 
>     >> Python code has a number of cases where a higher level routine
>     >> traps an exception and reraises it with new information and
>     >> losing the lower level error detail in the process.
> 
>     KY> How about just holding on to the original exception and
>     KY> storing it as an attribute (e.g. "cause") on the new
>     KY> exception?
> 
> +1.  You may actually have a chain of exceptions so being able to
> follow these using a well-known attribute is a good thing.

It would be good to not only hold on to the exception, but to
be able to tell where the original exception originated, i.e. to
be able get a traceback for both exceptions.

See
http://mail.python.org/pipermail/python-dev/2002-March/021846.html
for an old thread about this topic.

So what would exception chaining mean for the C API?

PyErr_SetString(PyExc_TypeError, "wrong type");
PyErr_SetString(PyExc_TypeError, "can't iterate");

The second call would normalize the first exception and attach
it to the second as the cause attribute? But then the second
exception would have to be normalized too.

Bye,
    Walter Dörwald