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

Pierre Rouleau pieroul at attglobal.net
Sun Dec 1 15:34:01 EST 2002


John Roth wrote:
> "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.
Bengt Richter's reply just above in the thread mainly answered my question.
I posted a simple function that does what i was looking for.
Thanks!



-- 
         Pierre Rouleau





More information about the Python-list mailing list