错误堆栈信息

1. 说明

一般我们使用try方法来过滤报错信息;
不过能报错信息只显示报错;
不能定位到代码哪行报错,报个文件报错

2. 正常显示报错

def func():
    try:
        i = 123
        for i in range(10):
            pass
        int('aaa')
    except Exception as e:
        print(e)

func()
"""
错误显示:
invalid literal for int() with base 10: 'aaa'
"""

3. 使用错误堆栈信息

import traceback

def func2():
    try:
        i = 123
        for i in range(10):
            pass
        int('aaa')
    except Exception as e:
        print(traceback.format_exc())

func2()

"""
错误显示:
Traceback (most recent call last):
  File "E:/development/autoclient/错误的堆栈.py", line 28, in func2
    int('aaa')
ValueError: invalid literal for int() with base 10: 'aaa'
"""

  

 

 

  

 

posted @ 2019-02-15 15:31  Pythia丶陌乐  阅读(1198)  评论(0编辑  收藏  举报