Details on exceptions

John Roth newsgroups at jhrothjr.com
Fri Nov 21 07:26:00 EST 2003


"Rene Pijlman" <reply.in.the.newsgroup at my.address.is.invalid> wrote in
message news:l1mrrvonmkbmmss8r8egf82bhqvbmn01ge at 4ax.com...
> 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.

I believe the actual answer is that what's assigned is the value of
the 'args' attribute, which is what is specified on the throw statement.
In this case, the args attribute is a tuple of two values - see the
discussion of Exception EnvironmentError (which is the base class
for IOError) in the Library Reference.

In this case, I'd probably agree: I'd capture the actual
exception because it probably contains additional data
like the file name.

John Roth
>
> -- 
> René Pijlman






More information about the Python-list mailing list