python try except返回异常的信息字符串代码实例

 更新时间:2019年08月15日 17:08:59   作者:贫民窟里的程序高手  
这篇文章主要介绍了python try 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',)
('month must be in 1..12',)
('day is out of range for month',)
datetime.datetime(2018, 4, 20, 0, 0)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • python中对列表的删除和添加方法详解

    python中对列表的删除和添加方法详解

    这篇文章主要为大家详细介绍了python中对列表的删除和添加方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • Python+Redis实现布隆过滤器

    Python+Redis实现布隆过滤器

    布隆过滤器(Bloom Filter)是1970年由布隆提出的。它实际上是一个很长的二进制向量和一系列随机映射函数。这篇文章主要介绍了Python+Redis实现布隆过滤器,需要的朋友可以参考下
    2019-12-12
  • 如何使用Python自动控制windows桌面

    如何使用Python自动控制windows桌面

    这篇文章主要介绍了如何使用Python自动控制windows桌面,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • Python实现用户名和密码登录

    Python实现用户名和密码登录

    这篇文章主要为大家详细介绍了Python实现用户名和密码登录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Python GUI编程学习笔记之tkinter事件绑定操作详解

    Python GUI编程学习笔记之tkinter事件绑定操作详解

    这篇文章主要介绍了Python GUI编程学习笔记之tkinter事件绑定操作,结合实例形式分析了Python GUI编程tkinter事件绑定常见操作技巧与使用注意事项,需要的朋友可以参考下
    2020-03-03
  • 手残删除python之后的补救方法

    手残删除python之后的补救方法

    这篇文章主要介绍了手残删除python之后的补救方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • 一篇文章带你搞懂Python类的相关知识

    一篇文章带你搞懂Python类的相关知识

    今天我们要说的是面向对象的核心-----类,类能帮我们把复杂的事情变得有条理,有顺序,希望大家通过学习类能改善自己的编码风格,使代码变得更为好看,更加通俗易懂,需要的朋友可以参考下
    2021-05-05
  • python爬虫selenium和phantomJs使用方法解析

    python爬虫selenium和phantomJs使用方法解析

    这篇文章主要介绍了python爬虫selenium和phantomJs使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Python第三方库Click快速使用详解

    Python第三方库Click快速使用详解

    这篇文章主要介绍了Python第三方库Click的相关资料,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2024-12-12
  • Python的3种运行方式:命令行窗口、Python解释器、IDLE的实现

    Python的3种运行方式:命令行窗口、Python解释器、IDLE的实现

    这篇文章主要介绍了Python的3种运行方式:命令行窗口、Python解释器、IDLE的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10

最新评论