[Python-3000] The future of exceptions

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sat Sep 2 20:04:08 CEST 2006

Georg Brandl <g.brandl at gmx.net> writes:

> * Could the traceback be made an attribute of the exception?
> * What about exception chaining?
> Something like this comes to mind::
>     try:
>         whatever
>     except ValueError as err:
>         raise CustomException("Something went wrong", prev=err)

In my language the traceback is materialized from the stack only
if needed (typically when an exception escapes from the toplevel),
and it includes the history of other exceptions thrown from exception
handlers, intermingled with source locations. The stack is not
physically unwound until an exception handler completes successfully,
so the data is available until then.

For example the above (without storing prev) would include:
- locations of active functions leading to whatever
- the location of whatever when the value error is raised
- exception: the ValueError instance
- the location of raise CustomException
- exception: the CustomException instance

Printing the stack trace recognizes when the same exception object is
reraised again, and prints this as a propagation instead of repeating
the exception description.

Of course this design is suitable only if the previous exception
is used merely for printing the stack trace, not for unpacking and
examining by the program.

I don't know how Python stack traces are implemented, so I have no
idea whether this would be practical for Python, assuming it would be
desirable at all.

   __("<         Marcin Kowalczyk
   \__/       qrczak at knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/

More information about the Python-3000 mailing list