sys.exit
Chuck Esterbrook
echuck at mindspring.com
Mon Apr 24 17:57:48 EDT 2000
The short question:
If I catch a SystemExit exception (because of an invocation of sys.exit(code)), how do I examine the argument to sys.exit()?
The long story:
I have a try..except like so:
try:
execfile(filename, namespace)
except:
# handle problems
I'd like to treat the case of "sys.exit(0)" as a special case in the exception handler because I'm executing another script. If that script calls sys.exit(0), that's OK. If it throws any other kind of exception, I need to log it and report it.
My attempt in the "except:" is this:
self._errorOccurred = 1
excInfo = sys.exc_info()
if excInfo[0]==SystemExit:
data = excInfo[1]
if data==0 or data==None:
self._errorOccurred = 0
But "_errorOccured" is never set to 0, because 'data' isn't what I expected. Upon printing the "excInfo" tuple I get:
(<class exceptions.SystemExit at 80afc70>, <exceptions.SystemExit instance at 8110f58>, <traceback object at 811f540>)
And the attributes of the SystemExit instance are:
['__doc__', '__init__', '__module__']
I don't know how to get my hands on the exit code.
-Chuck
More information about the Python-list
mailing list