On Jul 7, 4:13 pm, samwyse <[EMAIL PROTECTED]> wrote: > On Jul 5, 8:53 am, Christoph Zwerschke <[EMAIL PROTECTED]> wrote: > > > What is the best way to re-raise any exception with a message > > supplemented with additional information (e.g. line number in a > > template)? [...] > That leaves the issue of the name being changed for > UnicodeDecodeError, which might be fixable by diddling with __name__ > properties. Or perhaps SorryEx needs to be a factory that returns > exception classes; the last line would be "SorryEx(e)()". I'll have > to play with this a bit.
OK, the following mostly works. You probably want the factory to copy more of the original class into the SorryEx class each time, since someone catching an exception may expect to look at things besides its string representation. def SorryFactory(e): class SorryEx(Exception): def __init__(self): self._e = e def __getattr__(self, name): return getattr(self._e, name) def __str__(self): return str(self._e) + ", sorry!" SorryEx.__name__ = e.__class__.__name__ return SorryEx def test(code): try: code() except Exception, e: try: raise e.__class__, str(e) + ", sorry!" except TypeError: raise SorryFactory(e)() test(lambda: unicode('\xe4')) -- http://mail.python.org/mailman/listinfo/python-list