错误堆栈信息
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' """