propapating exceptions

Thomas Bryan tbryan at arlut.utexas.edu
Fri Jul 16 21:43:33 EDT 1999


Quinn Dunkan wrote:
> 
> I've lately found myself in the position of wanting to catch exceptions
> based on their attributes, rather than on the exception itself.  So (in
> mal's odbc module):
> 
> try:
>         some sql command
> except ODBC.MySQL.OperationalError, msg:
>         if msg[1] == CR_SERVER_GONE_ERROR: # (snarfed out of errmsg.h)
>                 restart server, try again
>         else:
>                 raise sys.exc_info()[0], sys.exc_info()[1]
> 
> It's a rather wordy way to pass the exception on without touching it,
> and I still lose the traceback info.  Is there a better way to pass on
> an exception as if it had never been caught?


I can't find the reference now, but I think that I read in 
the Python docs that calling raise without an argument inside 
an except, reraises the error.

That is, 

try:
        some sql command
except ODBC.MySQL.OperationalError, msg:
        if msg[1] == CR_SERVER_GONE_ERROR: # (snarfed out of errmsg.h)
                restart server, try again
        else:
                raise

Does that work?

-------------------------------------------
Tom Bryan
Applied Research Laboratories
University of Texas at Austin




More information about the Python-list mailing list