Re-raising exceptions with modified message

Christoph Zwerschke cito at online.de
Sun Jul 15 05:16:24 EDT 2007


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

Ok, here is another solution that does not depend on args:

def PoliteException(e):
     E = e.__class__
     class PoliteException(E):
         def __init__(self):
             for arg in dir(e):
                 if not arg.startswith('_'):
                     setattr(self, arg, getattr(e, arg))
         def __str__(self):
             return str(e) + ", sorry!"
     PoliteException.__name__ = E.__name__
     return PoliteException()

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



More information about the Python-list mailing list