Re-raising exceptions with modified message

Alex Popescu the.mindstorm.mailinglist at gmail.com
Fri Jul 6 00:06:50 CEST 2007


On Jul 6, 12:21 am, Christoph Zwerschke <c... at online.de> wrote:
> Kay Schluehr wrote:
> > If you are sure that the exception isn't caught on another level just
> > use the following showtraceback() function, manipulate it's output
> > slightly and terminate your program with sys.exit()
>
> That's what I want to avoid. In my case the error is displayed and
> evaluated in a completly different piece of software.
>
> -- Chris

Probably the simplest solution would be to create a new exception and
wrapping the old one and the additional info. Unfortunately,
this may have a huge impact on 3rd party code that was catching the
original exception.

So, I think you should create an utility factory-like function that is
either creating a new exception instance as the one caught
and with the additional information, or an utility that knows how to
modify the caught exception according to its type.
In the first case you will need somehow to tell to the new instance
exception the real stack trace, because by simply raising
a new one the original stack trace may get lost.

bests,

./alex
--
.w( the_mindstorm )p.




More information about the Python-list mailing list