Exception Handling in Python 3

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Sun Oct 24 02:22:39 EDT 2010


In message <mailman.176.1287896531.2218.python-list at python.org>, Steve 
Holden 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).

So what exactly is the problem? Exceptions are so easy to get wrong, it’s 
just trying to report more info in a complicated situation to help you track 
down the problem. Why is that bad?

> In a class's __getattr__() method this means that instead of being able
> to say
> 
>     try:
>         value = _attrs[name]
>     except KeyError:
>         raise AttributeError ...
> 
> I am forced to write
> 
>     if name not in _attrs:
>         raise AttributeError ...
>     value = _attrs[name]

I don’t see why. Presumably if you caught the exception in an outer try-
except clause, you would pick up your AttributeError, not the KeyError, 
right? Which is what you want, right?



More information about the Python-list mailing list