"reraise" & Exception Transformation (was Re: Modules implicitly exposing exceptions from other modules)

Skip Montanaro skip at mojam.com
Wed Feb 16 16:19:13 CET 2000


    Randall> SIDE POINT: 

    Randall> Sadly in Python, with this syntax we loose track of the stack
    Randall> frames between the original exception and this exception
    Randall> transformation (which would be useful to have if the client
    Randall> doesn't handle the error and we print a traceback).  But AFAICT
    Randall> this is a feature (or deficiency) of the language.

Watch out for Guido's time machine (again!).  The raise statement takes
three (all optional) objects.  If you catch the original exception's context 
inside the except block using sys.exc_info() you can pass the traceback
object as the third argument to raise, e.g.:

    try:
	whole_buncha_hooey()
    except socket.error:
        type, val, tb = sys.exc_info()
	raise NNTPError, NNTPError((val, my_interesting_local_state()), tb

When the NNTPerror exception is caught by the client it will see the stack
as it existed within the call to whole_buncha_hooey().

I think I have the syntax of the raise statement correct.  The reference
manual isn't as clear as it might be on this.  I'm sure someone will point
out any errors.

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
"Languages that change by catering to the tastes of non-users tend not to do
so well." - Doug Landauer




More information about the Python-list mailing list