格式化和捕获 sys.exc_info 条错误消息
Formatting and capturing sys.exc_info error messages
我正在尝试通过 ''.join(traceback.format_stack())
格式化并从 sys.exc_info 获取信息,但没有成功,关于如何从 sys.esc_info 获取所有信息的任何建议,成一个字符串。我最终需要将所有这些信息写入 table.
使用sys.exc_info()
得到异常三元组。要从跟踪中获取字符串,请使用 traceback.format_tb()
.
#!/usr/bin/env python
import sys
import traceback
try:
raise RuntimeError("This is an error")
except:
exception_type, exception_value, trace = sys.exc_info()
print(f"Exception type: {exception_type}") # <class 'RuntimeError'>
print(f"Exception value: {exception_value}") # This is an error
trace_string = "".join(traceback.format_tb(trace))
print(f"Stack trace:\n{trace_string}")
# File "/home/pierre/Whosebug.py", line 5, in <module>
# raise RuntimeError("This is an error")
我正在尝试通过 ''.join(traceback.format_stack())
格式化并从 sys.exc_info 获取信息,但没有成功,关于如何从 sys.esc_info 获取所有信息的任何建议,成一个字符串。我最终需要将所有这些信息写入 table.
使用sys.exc_info()
得到异常三元组。要从跟踪中获取字符串,请使用 traceback.format_tb()
.
#!/usr/bin/env python
import sys
import traceback
try:
raise RuntimeError("This is an error")
except:
exception_type, exception_value, trace = sys.exc_info()
print(f"Exception type: {exception_type}") # <class 'RuntimeError'>
print(f"Exception value: {exception_value}") # This is an error
trace_string = "".join(traceback.format_tb(trace))
print(f"Stack trace:\n{trace_string}")
# File "/home/pierre/Whosebug.py", line 5, in <module>
# raise RuntimeError("This is an error")