zoukankan      html  css  js  c++  java
  • Python中获取异常(Exception)信息

    异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置。下面介绍几种python中获取异常信息的方法,这里获取异常(Exception)信息采用try...except...程序结构。如下所示

    复制代码
    try:
    
      ...
    
    except Exception as e:
    
      ...
    复制代码

    1、str(e)

    返回字符串类型,只给出异常信息,不包括异常信息的类型,如1/0的异常信息

    'integer division or modulo by zero'

    2、repr(e)

    给出较全的异常信息,包括异常信息的类型,如1/0的异常信息

    "ZeroDivisionError('integer division or modulo by zero',)"

    3、e.message

    获得的信息同str(e)

    4、采用traceback模块

      需要导入traceback模块,此时获取的信息最全,与python命令行运行程序出现错误信息一致。使用traceback.print_exc()打印异常信息到标准错误,就像没有获取一样,或者使用traceback.format_exc()将同样的输出获取为字符串。你可以向这些函数传递各种各样的参数来限制输出,或者重新打印到像文件类型的对象。

    示例如下:

    复制代码
    import traceback
    
    print '########################################################'
    print "1/0 Exception Info"
    print '---------------------------------------------------------'
    try:
        1/0
    except Exception as e:
        print 'str(Exception):	', str(Exception)
        print 'str(e):		', str(e)
        print 'repr(e):	', repr(e)
        print 'e.message:	', e.message
        print 'traceback.print_exc():'; traceback.print_exc()
        print 'traceback.format_exc():
    %s' % traceback.format_exc()
    print '########################################################'
    print '
    ########################################################'  
    print "i = int('a') Exception Info"
    print '---------------------------------------------------------'
    try:
        i = int('a')
    except Exception as e:
        print 'str(Exception):	', str(Exception)
        print 'str(e):		', str(e)
        print 'repr(e):	', repr(e)
        print 'e.message:	', e.message
        print 'traceback.print_exc():'; traceback.print_exc()
        print 'traceback.format_exc():
    %s' % traceback.format_exc()
    print '########################################################' 
    复制代码

    示例结果

  • 相关阅读:
    vue Bus事件用法与bug
    移动端超出三行显示第三行显示省略号+查看全部
    Vuex
    vue点击旋转,再点击复原
    vue项目(cli-3)替换浏览器logo
    悬浮广告(二)vue版本
    悬浮广告(一)html版本
    vue下获得经纬度省市区
    一个可以输出当前移动设备机型(安卓,ios)系统版本的html页面
    谱聚类(spectral clustering)原理总结
  • 原文地址:https://www.cnblogs.com/hushaojun/p/6227271.html
Copyright © 2011-2022 走看看