Re-raising exceptions with modified message

Christoph Zwerschke cito at online.de
Sun Jul 15 05:01:51 EDT 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)

try:
     unicode('\xe4')
except Exception, e:
     p = PoliteException(e)
     assert p.reason == e.reason
     raise p



More information about the Python-list mailing list