Python exceptions: is there a way to find theexceptionattributes?

Pierre Rouleau pieroul at attglobal.net
Sat Nov 30 22:16:22 EST 2002


John Hunter wrote:
>>>>>>"Pierre" == Pierre Rouleau <pieroul at attglobal.net> writes:
>>>>>
> 
> 
>     Pierre> I was looking for a quick way to look at exception
>     Pierre> attributes from within my programming environment when i
>     Pierre> have to decide what exception i should catch or which one
>     Pierre> i should raise and then what information i should pass
>     Pierre> inside the exception object.
> 
> Within recent interactive python shells (which include pydoc.help by
> default), you can do, for example:
> 
>   help(Exception)
>   help(StandardError)
>   help(IOError)
>   help(StandardError)
>   help(AttributeError)
>   help(KeyError)
> 
> With some shells (eg, ipython) you can drop the parentheses.  Is this
> what you are looking for?
> 
>
Not really.  I would like to be able to know what my code can retreive 
from a given exception object (its attributes) without having to look 
into the code.  Exceptions are different than other objects in the sense 
that I can't simply create one and list its attributes.

For example, on Python 2.2, if you run help(IOError) you get:


Help on class IOError in module exceptions:

class IOError(EnvironmentError)
  |  I/O operation failed.
  |
  |  Method resolution order:
  |      IOError
  |      EnvironmentError
  |      StandardError
  |      Exception
  |
  |  Data and non-method functions defined here:
  |
  |  __doc__ = 'I/O operation failed.'
  |
  |  __module__ = 'exceptions'
  |
  |  ----------------------------------------------------------------------
  |  Methods inherited from EnvironmentError:
  |
  |  __init__(...)
  |
  |  __str__(...)
  |
  |  ----------------------------------------------------------------------
  |  Methods inherited from Exception:
  |
  |  __getitem__(...)


There is nothing in that information to tell anyone that IOError 
exceptions have 3 attributes: errno, filename and strerror.

In my question, I show a way to find out the list of attributes.  The 
code is generating the exception, cathing the exception object and use 
dir() on the exception object instance to list its attributes.  By 
visual inspection it becomes easy to find their names:

 >>> try:
...   f=open("/invalid/invalid.oops")
... except IOError, e:
...    print dir(e)
...
['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args', 
'errno', 'filename', 'strerror']
 >>>

But lets assume i did not know how to generate an IOError exception.  I 
would have to look into the documentation to find out what it is (or its 
source code if it is available).  If it is a builtin exception I would 
have to find out how to generate the exception before I could write a 
small code snippet (like the one above) to be able to get one object for 
the type of exception I am interested in.

I am just trying to find out if there is a faster way than that.  A 
method that does not involve all of this cogitation.  This way, if one 
day, with a new release of Python i want to quickly know what are the 
attributes to this new exception, I would quickly find out.


-- 
         Pierre Rouleau




More information about the Python-list mailing list