[Python-ideas] raising an exception type doesn't instantiate it until it gets caught

Nick Coghlan ncoghlan at gmail.com
Mon Nov 7 02:46:43 CET 2011


On Mon, Nov 7, 2011 at 11:38 AM, Gregory P. Smith <greg at krypto.org> wrote:
>
> On Tue, Nov 1, 2011 at 1:15 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>
>> No, the traceback info is added by the eval loop itself. Remember that
>> when you raise an exception *type* (rather than an instance), the
>> exception doesn't get instantiated until it gets caught somewhere -
>> the eval loop maintains the unwinding stack for the traceback as part
>> of the thread state until it is time to attach it to the exception
>> object.
>
> I did not know that.  Is there a good reason for doing this?  It seems
> unnecessarily complicated.

You'll have to ask Guido that one - it's been like that since long
before I got involved in hacking on the interpreter.

It's possibly a lingering artifact of the old "exceptions are just
strings" design, since traceback storage didn't get added to exception
instances until Py3k.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list