Python 2.7异常元组索引错误

时间:2014-01-11 03:21:31

标签: python exception

我想从Exception元组中获取错误代码。在Python 2.4中,下面的代码可以工作,但不适用于Python 2.7。运行Python 2.7时似乎 exceptionTuple 无法编入索引?有什么想法吗?

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结果:

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结果:

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 个答案:

答案 0 :(得分:3)

我不熟悉Python 2.4,但我尝试在Python 2.7.5中使用你的代码。

如果您print type(failureData)发现异常,它会告诉您:

<class '__main__.CRaiseException'>

这显然意味着failureData是一个类而不是一个元组 - 因此你不能用[0] [2]索引它。

只需改变:

failCode = failureData[0][2]

failCode = failureData.failureData[0][2]

这一改变给了我这个结果:

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

与Python 2.4相同。

希望它有所帮助。

修改

构建代码的更好方法是避免在类和main中使用相同的变量名。看起来好像这样做:

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()

然后你也避免了我原先建议的丑陋failureData.failureData[0][2]情况。