[Python-3000] The future of exceptions

Michael Chermside mcherm at mcherm.com
Fri Sep 8 18:45:50 CEST 2006


Marcin Kowalczyk writes:
> In my language the traceback is materialized from the stack only
> if needed [...] The stack is not
> physically unwound until an exception handler completes successfully,
> so the data is available until then.

Jim Jewett writes:
> Even today, if a StopIteration() participates in a reference cycle,
> then it won't be reclaimed until the next gc run.  I'm not quite sure
> which direction should be a weakref, but I think it would be
> reasonable for the cycle to get broken when an catching except block
> exits without reraising.

When thinking about these things, don't forget that in Python an
exception handler can perform complicated actions, including invoking
new functions and possibly raising new exceptions. Any solution should
allow the following code to work "properly":

   # -- WARNING: demo code, not tested

   def logError(msg):
       try:
           errorChannel.write(msg)
       except IOError:
           pass

   try:
       callSomeCode()
   except SomeException as err:
       msg = str(msg)
       logError(msg)
       raise msg


By "properly" I mean that that when callSomeCode() raises SomeException
the uncaught exception will cause the program should print a stacktrace
which should correcly show the stack frame of callSomeCode(). This
should happen regardless of whether errorChannel raised an IOError.
In the process, though, we (1) added new frames to the stack, and (2)
successfully exited an error handler (the one for IOError).

It is work to provide this feature but without it Python programmers
cannot freely use any code they like within exception handlers, which
I think is an important feature. It doesn't necessarily imply that
the traceback be materialized immediately upon exception creation
(which is undesirable because we want exceptions lightweight enough
to use for things like for loop control!)... but it might mean that
pieces of the stack frame need hang around as long as the exception
itself does.

-- Michael Chermside



More information about the Python-3000 mailing list