Exception Handling in Python 3

Steve Holden steve at holdenweb.com
Sun Oct 24 14:28:33 CEST 2010

On 10/24/2010 4:48 AM, Martin v. Loewis wrote:
> Am 24.10.2010 07:01, schrieb Steve Holden:
>> I was somewhat surprised to discover that Python 3 no longer allows an
>> exception to be raised in an except clause (or rather that it reports it
>> as a separate exception that occurred during the handling of the first).
> I think you are misinterpreting what you are seeing. The exception being
> raised actually *is* an attribute error, and it actually is the
> attribute error that gets reported. It's only that reporting an
> exception that has a __context__ first reports the context, then reports
> the actual exception.
I don't believe I *am* misinterpreting it. The fact of the matter is
that the context is irrelevant to the user, and there should be some way
to suppress it to avoid over-complicating the traceback.

This behavior is quite reasonable during testing, but I would prefer to
exclude an explicit raise directly in the except handler since that is
hardly to be construed as accidental (whereas an exception in a function
called in the handler perhaps *should* be reported).

> You may now wonder whether it is possible to set __context__ to None
> somehow. See PEP 3134:
> Open Issue: Suppressing Context
>     As written, this PEP makes it impossible to suppress '__context__',
>     since setting exc.__context__ to None in an 'except' or 'finally'
>     clause will only result in it being set again when exc is raised.
I have already read that. Peter Otten has separately explained how to
suppress the behavior using sys.excepthook, which appears to be a
halfway satisfactory solution.

Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17       http://us.pycon.org/
See Python Video!       http://python.mirocommunity.org/
Holden Web LLC                 http://www.holdenweb.com/

More information about the Python-list mailing list