python try except 出现异常时,except 中如何返回异常的信息字符串
https://docs.python.org/3/tutorial/errors.html#handling-exceptions
https://docs.python.org/3/library/exceptions.html#ValueError
try:
int("x")
except Exception as e:
'''异常的父类,可以捕获所有的异常'''
print(e)
# e变量是Exception类型的实例,支持__str__()方法,可以直接打印。
invalid literal for int() with base 10: 'x'
try:
int("x")
except Exception as e:
'''异常的父类,可以捕获所有的异常'''
print(e.args)
# e变量有个属性是.args,它是错误信息的元组。
("invalid literal for int() with base 10: 'x'",)try: datetime(2017,2,30)except ValueError as e: print(e) day is out of range for monthtry: datetime(22017,2,30)except ValueError as e: print(e) year 22017 is out of rangetry: datetime(2017,22,30)except ValueError as e: print(e) month must be in 1..12e = Nonetry: datetime(2017,22,30)except ValueError as e: print(e) month must be in 1..12e
# e这个变量在异常过程结束后即被释放,再调用也无效
Traceback (most recent call last): File "<input>", line 1, in <module>NameError: name 'e' is not defined
errarg = None
try:
datetime(2017,22,30)
except ValueError as errarg:
print(errarg)
month must be in 1..12
errarg
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'errarg' is not defined
try:
datetime(2017,22,30)
except ValueError as errarg:
print(errarg.args)
# ValueError.args 返回元组
('month must be in 1..12',)
message = None
try:
datetime(2017,22,30)
except ValueError as errarg:
print(errarg.args)
message = errarg.args
('month must be in 1..12',)
message
('month must be in 1..12',)
try:
datetime(2017,22,30)
except ValueError as errarg:
print(errarg.args)
message = errarg
('month must be in 1..12',)
message
ValueError('month must be in 1..12',)
str(message)
'month must be in 1..12'
分析异常信息,并根据异常信息的提示做出相应处理:
try:
y = 2017
m = 22
d = 30
datetime(y,m,d)
except ValueError as errarg:
print(errarg.args)
message = errarg
m = re.search(u"month", str(message))
if m:
dt = datetime(y,1,d) ('month must be in 1..12',)
dt
datetime.datetime(2017, 1, 30, 0, 0)
甚至可以再except中进行递归调用:
def validatedate(y, mo, d):
dt = None
try:
dt = datetime(y, mo, d)
except ValueError as e:
print(e.args)
print(str(y)+str(mo)+str(d))
message = e
ma = re.search(u"^(year)|(month)|(day)", str(message))
ymd = ma.groups()
if ymd[0]:
dt = validatedate(datetime.now().year, mo, d)
if ymd[1]:
dt = validatedate(y, datetime.now().month, d)
if ymd[2]:
dt = validatedate(y, mo, datetime.now().day)
finally:
return dt validatedate(20199, 16, 33)
('year 20199 is out of range',)
201991633
('month must be in 1..12',)
20181633
('day is out of range for month',)
2018433
datetime.datetime(2018, 4, 20, 0, 0)
python try except 出现异常时,except 中如何返回异常的信息字符串的更多相关文章
- Python之uiautomation模块-获取CMD窗口中所打印的文字信息
当我们想以自动化的方式操作软件,以提高办公或测试效率时,有许多成熟的工具,比如针对Web端应用的Selenium.针对移动端应用的Appium.那么,PC端(Windows)桌面应用,又改如何处理呢? ...
- 【学习笔记】python 简单创建新建一个网络客户端,并返回相关的信息
#导入socket包 import socket #使用socket.socket创建socket连接 #AF_INET表示通信类型,与IPv4对应 #SOCK_STREAM对应TCP通信 print ...
- 在java中捕获异常时,使用log4j打印出错误堆栈信息
当java捕获到异常时,把详细的堆栈信息打印出来有助于我们排查异常原因,并修复相关bug,比如下面两张图,是打印未打印堆栈信息和打印堆栈信息的对比: 那么在使用log4j输出日志时,使用org.apa ...
- JVM反调调用优化,导致发生大量异常时log4j2线程阻塞
背景 在使用log4j2打日志时,当发生大量异常时,造成大量线程block问题的问题. 一个关于log4j2的高并发问题:https://blog.fliaping.com/a-high-concur ...
- Python之路-Python中文件和异常
一.文件的操作 open函数 在python中,使用open函数,打开一个已经存在的文件,或者新建一个新文件. 函数语法 open(name[, mode[, buffering[,encoding] ...
- 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...
- Python中出现的异常
简单的写几种我知道的关于Python中出现的异常含义,希望大神批评指正,我只是学软件开发的菜鸟,前面的路还很长,我会努力学习! 什么是异常? 异常既是一个事件,该事件会在程序执行过程中发生,影响了程序 ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- Python之路,Day25-----暂无正在更新中
Python之路,Day25-----暂无正在更新中
随机推荐
- Graphviz install the Windows for Scyther
1.在Pycharm 中使用Scyther工具的时候需要导入 graphviz 直接在 Interpreter 上安装的售后会报错,如果在 IDE上无法支架安装的库可以试图在控制台上安装,控制台上无法 ...
- idea中添加多级父子模块
在 IntelliJ IDEA 中,没有类似于 Eclipse 工作空间(Workspace)的概念,而是提出了Project和Module这两个概念. 在 IntelliJ IDEA 中Projec ...
- 开发一个代码的自动生成器,使用Jfinal4.3+Swagger+Sql
-- 所有表名select column_name 列名, data_type 字段类型, column_comment 字段注释 from information_schema.columns ...
- 基于Java+Selenium的WebUI自动化测试框架(十四)-----使用TestNG的Sample
到目前为止,我们所写的东西,都是集中在如何使用Selenium和Java来定位和读取元素.那么,到底如何具体开展测试,如何实现参数化,如何实现判定呢?下面,我们来看看Java应用程序的测试框架吧. 当 ...
- k8s安装之eventrouter.yaml
k8s的heapster项目中止以后, 事件收集的项目,就推荐使用https://github.com/heptiolabs/eventrouter项目了 Eventrouter This repos ...
- linux系统编程综合练习-实现一个小型的shell程序(一)
之前已经花了不少篇幅学习了linux系统编程的很多知识点:文件与io.进程.信号.管道,而零散的知识点,怎么能够综合的串接起来是学习的一个很重要的目的,当然最好的方式就是用所学的知识点做一个项目了,所 ...
- 《你们都是魔鬼吗》团队作业Beta冲刺---第一天
团队作业Beta冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 (1)掌握软件黑盒测试技术:(2)学会编制软件 ...
- robot framework 笔记(一)
背景: 平时使用rf时会用到一些方法,长时间不用就会忘记,本文用来记录当做自己的小笔记 内容持续更新中········ 一.robot framework 大小写转换 1.转换小写: ${low} E ...
- spring依赖注入实例
依赖注入: BL public class T01BL implements Serializable { private static final Log logger = LogFactory.g ...
- C# 4.0 新特性(.NET Framework 4.0 与 Visual Studio 2010 )
一.dynamic binding:动态绑定 在通过 dynamic 类型实现的操作中,该类型的作用是不在编译时类型检查,而是在运行时解析这些操作.dynamic 类型简化了对 COM API(例如 ...