Exception Handling in Python 3

Chris Rebert clp2 at rebertia.com
Sun Oct 24 07:26:23 CEST 2010


On Sat, Oct 23, 2010 at 10:01 PM, Steve Holden <steve at holdenweb.com> wrote:
> 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).
<snip>
> Give the traceback I expected and wanted in Python 2:
>
> Traceback (most recent call last):
>  File "<stdin>", line 4, in <module>
> AttributeError: No attribute 'nosuch'
>
> but in Python 3.1 the traceback looks like this:
>
> Traceback (most recent call last):
>  File "<stdin>", line 2, in <module>
> KeyError: 'nosuch'
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
>  File "<stdin>", line 4, in <module>
> AttributeError: No attribute 'nosuch'
>
> Modifying the code a little allows me to change the error message, but
> not much else:
<snip>
> What
> is the correct paradigm for this situation?

There doesn't seem to be one at the moment, although the issue isn't
very serious. Your Traceback is merely being made slightly longer/more
complicated than you'd prefer; however, conversely, what if a bug was
to be introduced into your exception handler? Then you'd likely very
much appreciate the "superfluous" Traceback info.

Your quandary is due to the unresolved status of the "Open Issue:
Suppressing Context" in PEP 3141
(http://www.python.org/dev/peps/pep-3134/ ). I guess you could start a
discussion about closing that issue somehow.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list