Re-raising exceptions with modified message

Christoph Zwerschke cito at
Sun Jul 15 11:01:51 CEST 2007

Christoph Zwerschke wrote:
> But my __getattr__ solution does not work either, since the attributes 
> are set to None when initialized, so __getattr__ is never called.

Here is a simple solution, but it depends on the existence of the args 
attribute that "will eventually be deprecated" according to the docs:

def PoliteException(e):
     E = e.__class__
     class PoliteException(E):
         def __str__(self):
             return str(e) + ", sorry!"
     PoliteException.__name__ = E.__name__
     return PoliteException(*e.args)

except Exception, e:
     p = PoliteException(e)
     assert p.reason == e.reason
     raise p

More information about the Python-list mailing list