基于python traceback实现异常的获取与处理
作者:jh11200 时间:2022-04-05 09:59:32
这篇文章主要介绍了基于python traceback实现异常的获取与处理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1、traceback.print_exc()
2、traceback.format_exc()
3、traceback.print_exception()
简单说下这三个方法是做什么用的:
1、print_exc():是对异常栈输出
2、format_exc():是把异常栈以字符串的形式返回,print(traceback.format_exc()) 就相当于traceback.print_exc()
3、print_exception():traceback.print_exc()实现方式就是traceback.print_exception(sys.exc_info()),可以点sys.exc_info()进
去看看实现
测试代码如下:
def func(a, b):
return a / b
if __name__ == '__main__':
import sys
import time
import traceback
try:
func(1, 0)
except Exception as e:
print('***', type(e), e, '***')
time.sleep(2)
print("***traceback.print_exc():*** ")
time.sleep(1)
traceback.print_exc()
time.sleep(2)
print("***traceback.format_exc():*** ")
time.sleep(1)
print(traceback.format_exc())
time.sleep(2)
print("***traceback.print_exception():*** ")
time.sleep(1)
traceback.print_exception(*sys.exc_info())
运行结果:
*** <class 'ZeroDivisionError'> division by zero ***
***traceback.print_exc():***
Traceback (most recent call last):
File "E:/HttpRunnerManager-jh/ApiManager/tests.py", line 42, in <module>
func(1, 0)
File "E:/HttpRunnerManager-jh/ApiManager/tests.py", line 33, in func
return a / b
ZeroDivisionError: division by zero
***traceback.format_exc():***
Traceback (most recent call last):
File "E:/HttpRunnerManager-jh/ApiManager/tests.py", line 42, in <module>
func(1, 0)
File "E:/HttpRunnerManager-jh/ApiManager/tests.py", line 33, in func
return a / b
ZeroDivisionError: division by zero
***traceback.print_exception():***
Traceback (most recent call last):
File "E:/HttpRunnerManager-jh/ApiManager/tests.py", line 42, in <module>
func(1, 0)
File "E:/HttpRunnerManager-jh/ApiManager/tests.py", line 33, in func
return a / b
ZeroDivisionError: division by zero
可以看出,三种方式打印结果是一样的。在开发时,做调试是很方便的。也可以把这种异常栈写入日志。
logging.exception(ex)
# 指名输出栈踪迹, logging.exception的内部也是包了一层此做法
logging.error(ex, exc_info=1)
# 更加严重的错误级别
logging.critical(ex, exc_info=1)
# 我直接copy的,未尝试。有时间会试下的
python 还有一个模块叫cgitb,输出的error非常详情。
try:
func(1, 0)
except Exception as e:
import cgitb
cgitb.enable(format='text')
func(1, 0)
来源:https://www.jianshu.com/p/01ed4b8d7d9a
标签:python,traceback,异常,获取,处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
微信公众平台开发教程⑤ 微信扫码支付模式介绍
2023-11-14 10:40:10
![](https://img.aspxhome.com/file/2023/9/94499_0s.png)
在Python反编译中批量pyc转 py的实现代码
2023-08-11 20:21:04
![](https://img.aspxhome.com/file/2023/2/61732_0s.png)
python基础教程之常用运算符
2021-09-07 00:11:47
对python当中不在本路径的py文件的引用详解
2022-12-17 15:12:42
Pandas.DataFrame转置的实现 <font color=red>原创</font>
2022-03-02 03:07:34
JavaScript setTimeout和setInterval的使用方法 说明
2023-08-31 10:48:19
自适应网页设计(Responsive Web Design)
2012-05-02 10:49:07
从p开始,循序渐进学习WEB标准
2008-03-08 18:53:00
python对矩阵进行转置的2种处理方法
2023-12-01 09:18:17
浅谈opencv自动光学检测、目标分割和检测(连通区域和findContours)
2023-04-15 09:25:18
![](https://img.aspxhome.com/file/2023/0/75890_0s.jpg)
Python中内置数据类型list,tuple,dict,set的区别和用法
2022-09-10 05:59:46
10条改进你的CSS代码的方法
2010-03-20 22:07:00
![](https://img.aspxhome.com/file/UploadPic/20103/20/best-css-02-10s.jpg)
关于utf-8格式中截取中英文字符串长度无效的问题
2008-11-25 14:00:00
python list.sort()根据多个关键字排序的方法实现
2021-05-22 03:16:09
Python动态生成多维数组的方法示例
2023-07-19 04:12:03
![](https://img.aspxhome.com/file/2023/1/90601_0s.png)
python 日志增量抓取实现方法
2022-01-08 15:43:07
python查找指定具有相同内容文件的方法
2023-08-14 17:20:04
python调试模式无响应解决案例
2021-03-27 00:23:18
![](https://img.aspxhome.com/file/2023/0/101280_0s.jpg)
python使用selenium打开chrome浏览器时带用户登录信息实现过程详解
2023-07-19 05:00:37
![](https://img.aspxhome.com/file/2023/8/60298_0s.jpg)
asp如何显示一个等待或欢迎信息?
2010-06-08 09:39:00