3

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

1 Answer 1

3

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.

EDIT:

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick answer. You are right failData = exceptionData.failureData or exceptionData.args will return the correct tuple. This is actually a snippet of a large code and I don't want to change failureData = exceptionData as there are so many occurrences in the actual code. I'm looking for a way to override the class Exception return such that it will be compatible to both Python 2.4 and Python 2.7.
I tried looking through the 2.4 documentation of user defined exceptions and came across this example: try: raise MyError(2*2) except MyError, e: print 'My exception occurred, value:', e.value - based on this example is seems weird that your code will work without actually accessing failureData directly on your exceptionData object (or so it seems to me). See User Defined Exceptions here: docs.python.org/2.4/tut/node10.html. But then again I am unfamiliar with 2.4 so I might be missing something. But you should be able to handle it the same way in both 2.4 and 2.7 as I see it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.