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