Re-raising exceptions with modified message

Christoph Zwerschke cito at online.de
Thu Jul 5 15:38:18 EDT 2007


Neil Cerutti wrote:
> You may need the traceback module to get at the error message, if
> trying to read e.message can fail.
> 
> Something like this mess here: ;)
> 
>    ...
>    except Exception, e:
>      etype, evalue, etb = sys.exc_info()
>      ex = traceback.format_exception_only(etype, evalue)
>      message = ex[0].partition(':')[2].strip()
>      raise etype, message+". Sorry!", etb
> 
> Note that the above will break for SyntaxError (who's message
> contains more than one line) and any kind of exception that
> doesn't inherit from Exception.

That's actually similar to what I was using in Kid already.

The problem is that there are some Exceptions which cannot be 
instantiated with a single string argument, such as UnicodeDeocdeError.
Please try the above with "unicode('\xe4')" instead of the dots.
Instead of re-raising the UnicodeDecodeError, you will get a TypeError 
because of this problem.

-- Chris




More information about the Python-list mailing list