change an exception's message and re-raise it

Phlip phlip2005 at gmail.com
Thu Dec 31 15:18:42 EST 2009


Pythonistas:

I need to do this:

  try:
     deep_arcane_layer()
  except e:
    e.message = 'the deep arcane layer says: ' + e.message
    raise e

The point is I need to augment that layer's exceptions with extra
information that I know about that layer.

I naturally cannot use the argless version of 'raise', because it only
re-raises whatever exception object is currently in play - and it
appears to be read-only or locked or something.

I also should not do this...

   raise Exception('blah ' + e.message)

...because that stripped off the exception type itself, and higher
layers need to know this.

My question is a common pattern in layered architectures, where
exceptions get decorated with extra info as they bubble up from the
engine room to the bridge. Any ideas?

--
  Phlip
  http://zeekland.zeroplayer.com/The_Elaborate_Art_of_Play_Part_1/1



More information about the Python-list mailing list