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

Randall Hopper aa8vb at yahoo.com
Wed Feb 16 12:07:34 EST 2000


Skip Montanaro:
 |    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

Hey, thanks!  Gotta love that time machine.  The syntax might scare the
newbies but I'm glad to see it's there.

I notice it doesn't indicate where the exception transformation took place.

   Traceback (innermost last):
     File "reraise.py", line 45, in ?
       inner()
     File "reraise.py", line 38, in inner
       outer()
     File "reraise.py", line 12, in outer
       raise IOError
   __main__.MyError: 1, Disk format failure

And it doesn't appear that you can add a frame to the traceback object
yourself using the traceback module.  But that's a small point I can live
with.

Thanks,

Randall

-- 
Randall Hopper
aa8vb at yahoo.com




More information about the Python-list mailing list