>>> def raise_example1():
... try:
... None()
... except TypeError, e:
... e.args = ('modified error message',) + e.args[1:]
... raise e
...
>>> def raise_example2():
... try:
... None()
... except TypeError, e:
... e.args = ('modified error message',) + e.args[1:]
... raise
...
>>> raise_example1()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 6, in raise_example1
TypeError: modified error message
>>> raise_example2()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in raise_example2
TypeError: modified error message
Note how the line numbers in the traceback are different.
The behaviour I want is from raise_example2, but I'm not sure if this is
documented behaviour, or if it is something I can rely on. Is it acceptable
to modify an exception before re-raising it?
--
Steven
Doesn't really answer the question you asked, but if you're using
Python 3.0, you might want to consider using the new `raise
AnException from AnotherException` feature rather than changing the
error message. See
http://docs.python.org/3.0/reference/simple_stmts.html#raise for
details.
Cheers,
Chris
--
I have a blog:
http://blog.rebertia.com
> I wish to catch an exception, modify the error message, and re-raise it.
> There are two ways I know of to do this, with subtly different effects:
>
>>>> def raise_example1():
> ... try:
> ... None()
> ... except TypeError, e:
> ... e.args = ('modified error message',) + e.args[1:]
> ... raise e
> ...
>>>> def raise_example2():
> ... try:
> ... None()
> ... except TypeError, e:
> ... e.args = ('modified error message',) + e.args[1:]
> ... raise
>
> The behaviour I want is from raise_example2, but I'm not sure if this is
> documented behaviour, or if it is something I can rely on. Is it
> acceptable
> to modify an exception before re-raising it?
I don't completely understand your question. Is it about the bare raise?
It is documented here:
http://docs.python.org/reference/simple_stmts.html#the-raise-statement
"If no expressions are present, raise re-raises the last exception that
was active in the current scope. [...] The three-expression form of raise
is useful to re-raise an exception transparently in an except clause, but
raise with no expressions should be preferred if the exception to be
re-raised was the most recently active exception in the current scope."
Or are you worried about modifying e.args? Hmm, it isn't explicitely
forbidden, so I assume it's allowed... I've done it a few times -usually
to provide more context to error messages- without bad consequences AFAICT.
--
Gabriel Genellina
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/
> En Fri, 06 Mar 2009 04:29:16 -0200, Steven D'Aprano <st...@pearwood.info>
> escribió:
...
>> The behaviour I want is from raise_example2, but I'm not sure if this is
>> documented behaviour, or if it is something I can rely on. Is it
>> acceptable to modify an exception before re-raising it?
>
> I don't completely understand your question. Is it about the bare raise?
> It is documented here:
> http://docs.python.org/reference/simple_stmts.html#the-raise-statement
Yes, I read that before posting (waves to Steve Holden *wink*), so I
understood that raise on it's own re-raises the current exception.
> Or are you worried about modifying e.args? Hmm, it isn't explicitely
> forbidden, so I assume it's allowed... I've done it a few times -usually
> to provide more context to error messages- without bad consequences
> AFAICT.
That is precisely my concern. Sorry for not explaining myself better.
Okay, if other people are doing it, I'm happy to rely on it as something
which shouldn't just go away without warning. Thanks to everyone who
replied.
--
Steven
> Okay, if other people are doing it, I'm happy to rely on it as
> something which shouldn't just go away without warning. Thanks to
> everyone who replied.
If you only want to modify the exception's message string, the
‘message’ attribute is standard IIRC. No need to re-write the entire
‘args’.
--
\ “Humanity has advanced, when it has advanced, not because it |
`\ has been sober, responsible, and cautious, but because it has |
_o__) been playful, rebellious, and immature.” —Tom Robbins |
Ben Finney
> Okay, if other people are doing it, I'm happy to rely on it as something
> which shouldn't just go away without warning.
At least, it will not go away without fuss.
I would guess that modifying the message is an intended use of bare
'raise', but that is not documented.