I want to get the error code from the Exception tuple. In Python 2.4 the code below works but not in Python 2.7. It seems the exceptionTuple when running Python 2.7 cannot be indexed? Any idea?
import traceback
import sys
import types
class CRaiseException(Exception):
'''info'''
args = []
def __init__(self, exceptionTuple):
print "__init__"
self.failureData = exceptionTuple
self.args = self.failureData # Becomes excptionData.args
def __str__(self):
print "__str__"
return str(self.failureData)
def __args__(self):
print "__args__"
return tuple(self.failureData)
def raiseException(code, msg='', exceptionType = CRaiseException):
failureData = (('Fail message', 0, 11044, 0), 22.0, (0.005, 0.001))
raise exceptionType(failureData)
if __name__ == '__main__':
try:
print sys.version
raiseException(12345, 'skip TrackCleanup and raise to do serial format.')
except CRaiseException, exceptionData:
try:
failureData = exceptionData
print "failureData = %s" %str(failureData)
failCode = failureData[0][2]
print "failCode = %s" %str(failCode)
print "Tuple exceptionData -- PASS"
except:
print "Non-tuple exceptioData -- FAIL"
print "Traceback %s" %traceback.format_exc()
Python 2.4 result:
2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)]
__init__
__str__
failureData = (('Fail message', 0, 11044, 0), 22.0, (0.0050000000000000001, 0.001))
failCode = 11044
Tuple exceptionData -- PASS
Python 2.7 result:
2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
__init__
__str__
failureData = (('Fail message', 0, 11044, 0), 22.0, (0.005, 0.001))
Non-tuple exceptioData -- FAIL
Traceback Traceback (most recent call last):
File "D:\Python\raiseException.py", line 51, in <module>
failCode = failureData[0][2] # Works in Python 2.4 but not in Python 2.7
IndexError: tuple index out of range