PYTHON输出捕获异常的堆栈
代码
# -*- coding: utf-8 -*-
import sys
import traceback
if __name__ == '__main__':
try:
raise Exception('This is an actively thrown exception')
except Exception:
print('Exception content: \n{}'.format(traceback.format_exc()))
sys.exit(0)
说明
输出捕获异常的堆栈参考print的输出语句。
输出
Exception content:
Traceback (most recent call last):
File "main.py", line 7, in <module>
raise Exception('This is an actively thrown exception')
Exception: This is an actively thrown exception