Re-raising exceptions with modified message

Christoph Zwerschke cito at online.de
Thu Jul 5 09:53:34 EDT 2007


What is the best way to re-raise any exception with a message 
supplemented with additional information (e.g. line number in a 
template)? Let's say for simplicity I just want to add "sorry" to every 
exception message. My naive solution was this:

try:
     ...
except Exception, e:
     raise e.__class__, str(e) + ", sorry!"

This works pretty well for most exceptions, e.g.

 >>> try:
...     1/0
... except Exception, e:
...     raise e.__class__, str(e) + ", sorry!"
...
Traceback (most recent call last):
   File "<stdin>", line 4, in <module>
ZeroDivisionError: integer division or modulo by zero, sorry!

But it fails for some exceptions that cannot be instantiated with a 
single string argument, like UnicodeDecodeError which gets "converted" 
to a TypeError:

 >>> try:
...     unicode('\xe4')
... except Exception, e:
...     raise e.__class__, str(e) + ", sorry!"
...
Traceback (most recent call last):
   File "<stdin>", line 4, in <module>
TypeError: function takes exactly 5 arguments (1 given)

Another approach is using a wrapper Extension class:

class SorryEx(Exception):
     def __init__(self, e):
         self._e = e
     def __getattr__(self, name):
         return getattr(self._e, name)
     def __str__(self):
         return str(self._e) + ", sorry!"

try:
     unicode('\xe4')
except Exception, e:
     raise SorryEx(e)

But then I get the name of the wrapper class in the message:

__main__.SorryEx: 'ascii' codec can't decode byte 0xe4 in position 0: 
ordinal not in range(128), sorry!

Yet another approach would be to replace the __str__ method of e, but 
this does not work for new style Exceptions (Python 2.5).

Any suggestions?

-- Chris





More information about the Python-list mailing list