Python exceptions: is there a way to find theexceptionattributes?
Bengt Richter
bokr at oz.net
Sun Dec 1 00:03:18 EST 2002
On Sat, 30 Nov 2002 22:16:22 -0500, Pierre Rouleau <pieroul at attglobal.net> wrote:
>
>
>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.
>
>
You could just mechanize your code snippet for all the official exceptions:
>>> import exceptions
>>> for xname in [x for x in dir(exceptions) if not x.startswith('_')]:
... x = exceptions.__dict__.get(xname)
... if x is None: print 'Nothing for %s' % x
... try:
... raise x
... except x, e:
... print '%s:\n %s' %(x.__name__,[a for a in dir(e) if not a.startswith('_')])
...
but I agree that there ought to be some visibility in documentation somewhere. Maybe there is?
Regards,
Bengt Richter
More information about the Python-list
mailing list