Re-raising exceptions with modified message
samwyse at gmail.com
Sat Jul 7 23:33:48 CEST 2007
On Jul 7, 4:13 pm, samwyse <samw... at gmail.com> wrote:
> On Jul 5, 8:53 am, Christoph Zwerschke <c... at online.de> 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
self._e = e
def __getattr__(self, name):
return getattr(self._e, name)
return str(self._e) + ", sorry!"
SorryEx.__name__ = e.__class__.__name__
except Exception, e:
raise e.__class__, str(e) + ", sorry!"
More information about the Python-list