其他分享
首页 > 其他分享> > 打印异常堆栈

打印异常堆栈

作者:互联网

import traceback
def fun(a,b):
    return a/b
try:
    fun(1,0)
except Exception as e:
    traceback.print_exc()
    #或者得到堆栈字符串信息
    info = traceback.format_exc()
    print(info)
    traceback.print_exc()

日志输出如下

:\python3.7.5\python.exe D:/code/flask_demo/bb.py
Traceback (most recent call last):
  File "D:/code/flask_demo/bb.py", line 5, in <module>
    fun(1,0)
  File "D:/code/flask_demo/bb.py", line 3, in fun
    return a/b
ZeroDivisionError: division by zero

Traceback (most recent call last):
  File "D:/code/flask_demo/bb.py", line 5, in <module>
    fun(1,0)
  File "D:/code/flask_demo/bb.py", line 3, in fun
    return a/b
ZeroDivisionError: division by zero
Traceback (most recent call last):
  File "D:/code/flask_demo/bb.py", line 5, in <module>
    fun(1,0)
  File "D:/code/flask_demo/bb.py", line 3, in fun
    return a/b
ZeroDivisionError: division by zero

Process finished with exit code 0

标签:code,bb,flask,demo,打印,fun,堆栈,py,异常
来源: https://www.cnblogs.com/kxtomato/p/16414869.html