Python exceptions: is there a way to find the exception attributes?

John Roth johnroth at ameritech.net
Sun Dec 1 07:16:05 EST 2002


"Pierre Rouleau" <pieroul at attglobal.net> wrote in message
news:3DE98129.3040703 at attglobal.net...
>
>
> Pat Notz wrote:
> >>Is there a way to find out about the existence of these attributes
> >>without having to read the documentation, using the dir() function
or
> >>something else?
> >
> >
> > You can also cast the exception into a string which -- if the
> > exception class is properly designed -- should give you useful
> > information.  For example:
> >
> > try:
> >     open('/foo.bar')
> > except Exception, e:
> >     print 'Exception occurred: %s' % str(e)
> >
> > That's not exactly what you were asking for but maybe it'll help.
>
>
> Thanks, but I am more after a quick way to get a list (at least find
out
> about the list) of the attributes of the exception object without
having
> to write a code snippet that would raise such an exception.  Ideally,
I
> would like to write a function exceptionAttribute() that would return
a
> list of strings corresponding to each attribute for a given exception.

Unfortunately, as Eric Francis said, there isn't such a beast. The
closest
you could come would be a code browser that would capture the
raise statements for the exception in question, and find out what the
code put in it. However, you've already said that's not what you want
to do.

As far as documentation is concerned, see section 2.3 Built-in
Exceptions
in the Python Library Reference. It's not the clearest doc in the world,
but it does have all the information I know of somewhere on that page.
For example, 'args' is mentioned in the fourth paragraph, and the other
attributes are mentioned in the writeup on EnvironmentError, which
is the base class for IO error.

John Roth






More information about the Python-list mailing list