美文网首页
Python打印更详细的异常信息

Python打印更详细的异常信息

作者: w风行者s | 来源:发表于2019-10-25 11:11 被阅读0次

    打印Python异常信息的几种方式

    1 简单的异常信息

    try:
        a = 1/0
    except Exception as e:
        print(e)
    

    打印最简单的message信息:

    division by zero
    

    2 更完整的信息

    import traceback
    
    try:
        a = 1/0
    except Exception as e:
        print('str(e):\t', e)
        print('repr(e):\t', repr(e))
    
        print('traceback.format_exc():\n%s' % traceback.format_exc()) #字符串
        traceback.print_exc() #执行函数
    

    输出:

    str(e):      division by zero
    repr(e):     ZeroDivisionError('division by zero')
    traceback.format_exc():
    Traceback (most recent call last):
      File "/Users/ace/Play/test/异常信息.py", line 4, in <module>
        a = 1/0
    ZeroDivisionError: division by zero
    
    Traceback (most recent call last):
      File "/Users/ace/Play/test/异常信息.py", line 4, in <module>
        a = 1/0
    ZeroDivisionError: division by zero
    

    traceback.format_exc()traceback.print_exc()都可以打印完整的错误信息

    traceback.format_exc()返回值为字符串

    traceback.print_exc()是一个执行函数,直接在控制台打印错误信息

    相关文章

      网友评论

          本文标题:Python打印更详细的异常信息

          本文链接:https://www.haomeiwen.com/subject/yjksvctx.html