Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Modify an exception before re-raising it

3 views
Skip to first unread message

Steven D'Aprano

unread,
Mar 6, 2009, 1:29:16 AM3/6/09
to
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
...
>>> 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

Chris Rebert

unread,
Mar 6, 2009, 1:49:47 AM3/6/09
to Steven D'Aprano, pytho...@python.org

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

Gabriel Genellina

unread,
Mar 6, 2009, 2:18:42 AM3/6/09
to pytho...@python.org
En Fri, 06 Mar 2009 04:29:16 -0200, Steven D'Aprano <st...@pearwood.info>
escribió:

> 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

Steve Holden

unread,
Mar 6, 2009, 2:26:22 AM3/6/09
to pytho...@python.org
Yes, "raise" on its own is specifically documented (he says, without
consulting the documentation - but then if you didn't, why should I?) to
re-raise a current exception. I see no reason why you shouldn't modify
your exception before the re-raise. You certainly don't want to raise
the exception explicitly, since as you have just discovered that will
alter its traceback.

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/

Steven D'Aprano

unread,
Mar 6, 2009, 3:47:25 AM3/6/09
to
Gabriel Genellina wrote:

> 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

Ben Finney

unread,
Mar 6, 2009, 3:58:22 AM3/6/09
to
Steven D'Aprano <st...@pearwood.info> writes:

> 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

Terry Reedy

unread,
Mar 6, 2009, 1:03:07 PM3/6/09
to pytho...@python.org
Steven D'Aprano wrote:

> 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.

0 new messages