Missing something on exception handling in Python 3

Skip Montanaro skip at pobox.com
Tue Aug 27 01:00:36 EDT 2013


I found this question/answer on Stack Overflow:

http://stackoverflow.com/questions/15123137

but after fiddling around with it, I can't find a solution that works
for Python 3.2 and 3.3, let alone 2.x.  In 3.2, exceptions have both
__cause__ and __context__ attributes.  I tried setting both to None
(in 3.2), but I still get the full double traceback:

>>> try: 1/0
... except ZeroDivisionError:
...    exc = TypeError()
...    exc.__context__ = None
...    exc.__cause__ = None
...    raise exc
...
Error in sys.excepthook:
IndexError: tuple index out of range

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
TypeError

The sys.excepthook error is because I have a custom interactive
sys.excepthook, which is itself apparently broken in Python 3.2.

I've looked at PEP 409 and 415, but just get more confused.

*sigh*

Maybe I'll try again tomorrow when I've had a bit of sleep... and take
a closer look at Ethan's suggestion.

Skip



More information about the Python-list mailing list