Modify an exception before re-raising it

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

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:
.... try:
.... None()
.... except TypeError, e:
.... e.args = ('modified error message',) + e.args[1:]
.... raise e
.... .... try:
.... None()
.... except TypeError, e:
.... e.args = ('modified error message',) + e.args[1:]
.... raise
.... Traceback (most recent call last):
File "<stdin>", line 1, in ?
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?
 
C

Chris Rebert

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:
...     try:
...             None()
...     except TypeError, e:
...             e.args = ('modified error message',) + e.args[1:]
...             raise e
......     try:
...             None()
...     except TypeError, e:
...             e.args = ('modified error message',) + e.args[1:]
...             raise
...Traceback (most recent call last):
 File "<stdin>", line 1, in ?
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?

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
 
G

Gabriel Genellina

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:
... try:
... None()
... except TypeError, e:
... e.args = ('modified error message',) + e.args[1:]
... raise e
...... 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.
 
S

Steve Holden

Steven said:
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:
... try:
... None()
... except TypeError, e:
... e.args = ('modified error message',) + e.args[1:]
... raise e
... ... try:
... None()
... except TypeError, e:
... e.args = ('modified error message',) + e.args[1:]
... raise
... Traceback (most recent call last):
File "<stdin>", line 1, in ?
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?
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
 
S

Steven D'Aprano

Gabriel said:
En Fri, 06 Mar 2009 04:29:16 -0200, Steven D'Aprano <[email protected]>
escribió: ....

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

Terry Reedy

Steven said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top