Reputation: 51
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
Upvotes: 3
Views: 488
Reputation: 877
I am unfamiliar with Python 2.4 but I tried playing around with your code in Python 2.7.5.
If you print type(failureData)
where you catch your exception it will tell you this:
<class '__main__.CRaiseException'>
This clearly means that failureData is a class rather than a tuple - and as such you cannot index it with [0][2].
Just change:
failCode = failureData[0][2]
To
failCode = failureData.failureData[0][2]
That change give me this result:
2.7.5 (default, Sep 12 2013, 21:33:34)
__init__
__str__
failureData = (('Fail message', 0, 11044, 0), 22.0, (0.005, 0.001))
failCode = 11044
Tuple exceptionData -- PASS
Which is the same as you get with Python 2.4.
Hope it helps.
A better way to structure you code would be to avoid using the same variable names in both your class and your main. Looks much better to do it like this:
if __name__ == '__main__':
try:
print sys.version
raiseException(12345, 'skip TrackCleanup and raise to do serial format.')
except CRaiseException, exceptionData:
try:
failData = exceptionData.failureData
print "failureData = %s" %str(failData)
failCode = failData[0][2]
print "failCode = %s" %str(failCode)
print "Tuple exceptionData -- PASS"
except:
print "Non-tuple exceptioData -- FAIL"
print "Traceback %s" %traceback.format_exc()
Then you also avoid the ugly failureData.failureData[0][2]
situation that I suggested originally.
Upvotes: 3