Python 捕获警告


警告不是异常

捕获警告的三种方法
方法一

import warnings
warnings.filterwarnings('error')
try:
warnings.warn("deprecated", DeprecationWarning)
except Warning as e:
print(e)

方法二
import warnings
try:
warnings.warn("deprecated", DeprecationWarning)
except Warning as e:
print(e)
$ python3 -W error demo.py
deprecated
方法三
import warnings
def do_warning():
warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings(record=True) as w:
do_warning()
if len(w) >0:
print(w[0].message)



更多精彩推荐
☞用 Python 动态可视化,看看比特币这几年
☞小小几张图,把深度学习讲透彻
☞Python 搭建车道智能检测系统
☞网易伏羲论文入选 CVPR:AI 感知表情能力或实现巨大突破!
点分享 点收藏 点点赞 点在看
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/

随时掌握互联网精彩
赞助链接
排名
热点
搜索指数
- 1 铁肩担道义 历史鉴未来 7904622
- 2 中方对会谈结果满意吗?外交部回应 7809020
- 3 40℃高温要来了 7712027
- 4 中国经济必将破浪前行 7616404
- 5 中美双方降低超100%关税 7522516
- 6 英国首相斯塔默住所起火 7423938
- 7 李嘉诚卖港口 长和发布声明回应 7330887
- 8 雪碧悄悄换了配方?售后人员回应 7237912
- 9 黄晓明金世佳进博士复试 7142314
- 10 中美各取消91%关税 暂停24%关税 7048446