Re-raising exceptions with modified message

Neil Cerutti horpner at yahoo.com
Thu Jul 5 16:05:08 EDT 2007


On 2007-07-05, Christoph Zwerschke <cito at online.de> wrote:
> 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.

Crud. After my third answer, I'll finally understand the
question. Unfortunately, I only had two in me.

-- 
Neil Cerutti
Low Self-Esteem Support Group will meet Thursday at 7 to 8:30 p.m. Please use
the back door. --Church Bulletin Blooper



More information about the Python-list mailing list