1、python调试的时候获取异常信息

 import traceback

 print '########################################################'
print "1/0 Exception Info"
print '---------------------------------------------------------'
try:
1/0
except Exception, e:
print 'str(Exception):\t', str(Exception)
print 'str(e):\t\t', str(e)
print 'repr(e):\t', repr(e)
print 'e.message:\t', e.message
print 'traceback.print_exc():'; traceback.print_exc()
print 'traceback.format_exc():\n%s' % traceback.format_exc()
print '########################################################'
print '\n########################################################'
print "i = int('a') Exception Info"
print '---------------------------------------------------------'
try:
i = int('a')
except Exception, e:
print 'str(Exception):\t', str(Exception)
print 'str(e):\t\t', str(e)
print 'repr(e):\t', repr(e)
print 'e.message:\t', e.message
print 'traceback.print_exc():'; traceback.print_exc()
print 'traceback.format_exc():\n%s' % traceback.format_exc()
print '########################################################'

异常信息:

 ########################################################
1/0 Exception Info
---------------------------------------------------------
str(Exception): <type 'exceptions.Exception'>
str(e): integer division or modulo by zero
repr(e): ZeroDivisionError('integer division or modulo by zero',)
e.message: integer division or modulo by zero
traceback.print_exc():
Traceback (most recent call last):
File "tmp.py", line 7, in <module>
1/0
ZeroDivisionError: integer division or modulo by zero
traceback.format_exc():
Traceback (most recent call last):
File "tmp.py", line 7, in <module>
1/0
ZeroDivisionError: integer division or modulo by zero ######################################################## ########################################################
i = int('a') Exception Info
---------------------------------------------------------
str(Exception): <type 'exceptions.Exception'>
str(e): invalid literal for int() with base 10: 'a'
repr(e): ValueError("invalid literal for int() with base 10: 'a'",)
e.message: invalid literal for int() with base 10: 'a'
traceback.print_exc():
Traceback (most recent call last):
File "tmp.py", line 20, in <module>
i = int('a')
ValueError: invalid literal for int() with base 10: 'a'
traceback.format_exc():
Traceback (most recent call last):
File "tmp.py", line 20, in <module>
i = int('a')
ValueError: invalid literal for int() with base 10: 'a' ########################################################

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()将同样的输出获取为字符串。你可以向这些函数传递各种各样的参数来限制输出,或者重新打印到像文件类型的对象。

最新文章

  1. UEditor手动调节其宽度
  2. 和声搜索算法-python实现
  3. The P4 Language Specification v1.0.2 Header and Fields
  4. 如何使用CSL(翻译总结自TI官方文档)
  5. bzoj4197
  6. OC10_代理反向传值
  7. js异步脚本
  8. Bash 字符串处理命令
  9. 9.13noip模拟试题
  10. POJ(2784)Buy or Build
  11. QT5删除隐藏目录+隐藏文件
  12. Linux Shell脚本入门--awk命令详解
  13. Kattis - Aaah!
  14. 或许你不知道的10条SQL技巧(转自58沈剑原创)
  15. 【B2B】2015 年B2B的春天
  16. 【Code clone】Distributed Code Clone Detection Based on Index
  17. React中路由传参及接收参数的方式
  18. 使用cnpm 安装vue.js
  19. day09-数据库插入中文报错
  20. POJ3666 线性dp_离散化_贪心

热门文章

  1. Kylin使用笔记-1: 安装
  2. 【LOJ】 #2033. 「SDOI2016」生成魔咒
  3. 用 React 编写移动应用 React Native
  4. JavaScript基础-DAY2
  5. Linux-数据库4
  6. 关于PIP 总结和记忆巩固
  7. C++11中的raw string literals
  8. AppDelegate 方法介绍
  9. webpack入门(1)
  10. Go语言Web框架gwk介绍 (三)