python异常信息获取
1、python调试的时候获取异常信息
1 import traceback 2 3 print '########################################################' 4 print "1/0 Exception Info" 5 print '---------------------------------------------------------' 6 try: 7 1/0 8 except Exception, e: 9 print 'str(Exception):\t', str(Exception) 10 print 'str(e):\t\t', str(e) 11 print 'repr(e):\t', repr(e) 12 print 'e.message:\t', e.message 13 print 'traceback.print_exc():'; traceback.print_exc() 14 print 'traceback.format_exc():\n%s' % traceback.format_exc() 15 print '########################################################' 16 print '\n########################################################' 17 print "i = int('a') Exception Info" 18 print '---------------------------------------------------------' 19 try: 20 i = int('a') 21 except Exception, e: 22 print 'str(Exception):\t', str(Exception) 23 print 'str(e):\t\t', str(e) 24 print 'repr(e):\t', repr(e) 25 print 'e.message:\t', e.message 26 print 'traceback.print_exc():'; traceback.print_exc() 27 print 'traceback.format_exc():\n%s' % traceback.format_exc() 28 print '########################################################'
异常信息:
1 ######################################################## 2 1/0 Exception Info 3 --------------------------------------------------------- 4 str(Exception): <type 'exceptions.Exception'> 5 str(e): integer division or modulo by zero 6 repr(e): ZeroDivisionError('integer division or modulo by zero',) 7 e.message: integer division or modulo by zero 8 traceback.print_exc(): 9 Traceback (most recent call last): 10 File "tmp.py", line 7, in <module> 11 1/0 12 ZeroDivisionError: integer division or modulo by zero 13 traceback.format_exc(): 14 Traceback (most recent call last): 15 File "tmp.py", line 7, in <module> 16 1/0 17 ZeroDivisionError: integer division or modulo by zero 18 19 ######################################################## 20 21 ######################################################## 22 i = int('a') Exception Info 23 --------------------------------------------------------- 24 str(Exception): <type 'exceptions.Exception'> 25 str(e): invalid literal for int() with base 10: 'a' 26 repr(e): ValueError("invalid literal for int() with base 10: 'a'",) 27 e.message: invalid literal for int() with base 10: 'a' 28 traceback.print_exc(): 29 Traceback (most recent call last): 30 File "tmp.py", line 20, in <module> 31 i = int('a') 32 ValueError: invalid literal for int() with base 10: 'a' 33 traceback.format_exc(): 34 Traceback (most recent call last): 35 File "tmp.py", line 20, in <module> 36 i = int('a') 37 ValueError: invalid literal for int() with base 10: 'a' 38 39 ########################################################
3、说明:
1、str(e)
返回字符串类型,只给出异常信息,不包括异常信息的类型,如1/0的异常信息
'integer division or modulo by zero'
2、repr(e)
给出较全的异常信息,包括异常信息的类型,如1/0的异常信息
"ZeroDivisionError('integer division or modulo by zero',)"
3、e.message
获得的信息同str(e)
4、采用traceback模块
需要导入traceback模块,此时获取的信息最全,与python命令行运行程序出现错误信息一致。使用traceback.print_exc()打印异常信息到标准错误,就像没有获取一样,或者使用traceback.format_exc()将同样的输出获取为字符串。你可以向这些函数传递各种各样的参数来限制输出,或者重新打印到像文件类型的对象。