python异常信息获取
2024-09-22 02:13:00
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()将同样的输出获取为字符串。你可以向这些函数传递各种各样的参数来限制输出,或者重新打印到像文件类型的对象。
最新文章
- UEditor手动调节其宽度
- 和声搜索算法-python实现
- The P4 Language Specification v1.0.2 Header and Fields
- 如何使用CSL(翻译总结自TI官方文档)
- bzoj4197
- OC10_代理反向传值
- js异步脚本
- Bash 字符串处理命令
- 9.13noip模拟试题
- POJ(2784)Buy or Build
- QT5删除隐藏目录+隐藏文件
- Linux Shell脚本入门--awk命令详解
- Kattis - Aaah!
- 或许你不知道的10条SQL技巧(转自58沈剑原创)
- 【B2B】2015 年B2B的春天
- 【Code clone】Distributed Code Clone Detection Based on Index
- React中路由传参及接收参数的方式
- 使用cnpm 安装vue.js
- day09-数据库插入中文报错
- POJ3666 线性dp_离散化_贪心