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-----暂无正在更新中
随机推荐
- [https][tls] 如何使用wireshark查看tls/https加密消息--使用keylog
姊妹篇: [ipsec][strongswan] 使用wireshark查看strongswan ipsec esp ikev1 ikev2的加密内容 [https][tls] 如何使用wiresha ...
- 使用ImageMagick在Linux系统上截图
ImageMagick安装指令: sudo apt-get install imagemagick 安装完成后,输入 import screenshot.png 命令就可以开始截图.此时鼠标图标会变成 ...
- 2013.5.23 - KDD第三十五天
看完睡不觉得世间有点虚度,然后就构思了一下带带回儿去找中秋要跟她说的事情,大概就是这样的: 我 打算用paper来计算人与人之间的距离,比如说我跟郑茂和写过一篇文章,然后郑茂根韩冰和写过一篇文章, ...
- 华为云PaaS首席科学家:Cloud Native +AI,企业数字化转型的最佳拍档
近日,在2019华为全球分析师大会期间,华为云PaaS首席科学家熊英博士在+智能,见未来(华为云&大数据)的分论坛上,从云计算行业发展谈起,深入云原生发展趋势,对华为云智能应用平台做了深度解读 ...
- React系列,初识
学习react对于新手来说,还没有学react往往就会被webpack,npm等搞的晕头转向,所以我们今天就从最简单的方式入手 <script src="react.js"& ...
- win10 64下anaconda4.2.0(python3.5)
python环境:win10 64下anaconda4.2.0(python3.5).安装tensorflow过程是在Anaconda Prompt中进行安装 1:打开Anaconda Prompt ...
- 使用SikuliX定位Object(flash)元素
先说一下背景,这个是我们测试的系统上的一个上传文件的地方,但是用传统的selenium方法很难定位的到.具体的样子是下面这样的. 使用id等属性定位做点击操作好像不能直接操作.无奈之下,只好从网上找找 ...
- Codeforces D. Powerful array(莫队)
题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...
- linux 中截取字符串
shell中截取字符串的方法有很多中,${expression}一共有9种使用方法.${parameter:-word}${parameter:=word}${parameter:?word}${pa ...
- thrift 安装
1.下载 去官网 https://thrift.apache.org/download 下载两个文件,下载地址 http://archive.apache.org/dist/thrift/0.9.3/ ...