Details on exceptions

Rene Pijlman reply.in.the.newsgroup at my.address.is.invalid
Fri Nov 21 04:25:46 EST 2003


Derek Fountain:
>except IOError, (errno, strerror):
>
>The target is a tuple of 2 variable names so how can it bind "the exception
>object" to them? The documentation for try/except says that "the
>exception's parameter is assigned to the target". So what, exactly, is an
>"exception's parameter"?

"For an except clause with an expression, that expression is evaluated,
and the clause matches the exception if the resulting object is
``compatible'' with the exception. An object is compatible with an
exception if it is either the object that identifies the exception, or
(for exceptions that are classes) it is a base class of the exception, or
it is a tuple containing an item that is compatible with the exception."
http://www.python.org/doc/current/ref/try.html#try

I think it has to do with the introduction of exceptions-are-classes in
1.5, and backwards compatibility of older code: "This works because the
tuple-unpack semantics have been loosened to work with any sequence on the
right-hand size (see the section on Sequence Unpacking below), and the
standard exception classes can be accessed like a sequence (by virtue of
their __getitem__ method, see above)."
http://www.python.org/doc/essays/stdexceptions.html

I'd say don't do it. Use exception objects and catch them as objects.

-- 
René Pijlman




More information about the Python-list mailing list