Capturing Python run-time exceptions ...

Michael Ströder michael at
Mon Mar 26 00:27:10 MDT 2012

Richard Sharpe wrote:
> 2012/3/25 Richard Sharpe <realrichardsharpe at>:
>> I need to get the following errors:
>>       RuntimeError: (-1073741790, 'Access denied')
>> It looks like I can do something like:
>>    try:
>>        some statements
>>    except RuntimeError:
>>        other statements
>> It seems like there is a set there with two entries. How do I get
>> access to those values.
> OK, I found it in my Python book.
>     except RuntimeError, val:
> then val is the info that was printed by the default handler.

Nitpicking: val is an instance of an exception object.

The exception class could have its own __str__() method leading to any output
in the printed exception traceback. So what is printed in the traceback output
is not necessarily the exact type of the data you could extract by accessing
the object's attributes.

Furthermore for exception RuntimeError the Python 2.7 documentation says that
there's only a string associated with it. But the example above (-1073741790,
'Access denied') looks like a tuple which is perfectly possible:

>>> raise RuntimeError((123,'foobar'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: (123, 'foobar')

So you cannot assume that it's always a 2-tuple when RuntimeError is raised.

Maybe the Samba code should define a custom exception class (if not already
done) for which the caller can assume to find a tuple in the exception object.

If the caller is not interested in extracting the tuple items it could just
call str(val) to always get a string somewhat representing the exception
object (e.g. for logging).

Ciao, Michael.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2317 bytes
Desc: S/MIME Cryptographic Signature
URL: <>

More information about the samba-technical mailing list