Re: Re-raising exceptions with modified message
On Jul 7, 4:13 pm, samwyse <samw...@gmail. comwrote:
[...]
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(Excepti on):
def __init__(self):
self._e = e
def __getattr__(sel f, name):
return getattr(self._e , name)
def __str__(self):
return str(self._e) + ", sorry!"
SorryEx.__name_ _ = e.__class__.__n ame__
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') )
On Jul 7, 4:13 pm, samwyse <samw...@gmail. comwrote:
On Jul 5, 8:53 am, Christoph Zwerschke <c...@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)?
supplemented with additional information (e.g. line number in a
template)?
That leaves the issue of the name being changed for
UnicodeDecodeEr ror, 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.
UnicodeDecodeEr ror, 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.
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(Excepti on):
def __init__(self):
self._e = e
def __getattr__(sel f, name):
return getattr(self._e , name)
def __str__(self):
return str(self._e) + ", sorry!"
SorryEx.__name_ _ = e.__class__.__n ame__
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') )
Comment