Which exception has been used? (was: Which exception to use?)

Mongryong Mongryong at sympatico.ca
Thu Jan 30 18:04:26 EST 2003


> Suppose, as is always true, though, that I'm dealing
> with modules not under my control.  How do I catch
> "other"?  

Two ways that I know of:

1) sys.exc_info() returns a tuble (<class>, <instance>, <traceback>)

import sys
try:
	x = 5/0
except:
	print sys.exc_info()
	
2) traceback.print_exc(streamType)

import traceback
myLog = open("logfile", "w+")
try:
	x = 5/0
except:
	traceback.print_exc(myLog)

Now, hopefully you know about the "finally" claus:

ie.

try:
	mutex.acquire()
	doSomethingHeavyDuty()
except:
	excInfo = sys.exc_info()
	myErrorHandler.handler(excInfo)
finall:
	mutex.release()

Note: You should use sys.exc_info() right away just incase another
exception occurs - in that case, the old exception info will be lost.







More information about the Python-list mailing list