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

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


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.


def exceptionAttribute(theExceptionClass):
    """ returns the list of all attributes (as strings) for 
theExceptionClass"""
    # do whatever is required
    return theList




-- 
         Pierre Rouleau, ing.
         Hull, Québec





More information about the Python-list mailing list