当我用sys.excepthook捕获意外错误时
import sys
import traceback
def handleException(excType, excValue, trace):
print 'error'
traceback.print_exception(excType, excValue, trace)
sys.excepthook = handleException
h = 1
k = 0
print h/k
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出
error
Traceback (most recent call last):
File "test.py", line 13, in <module>
print h/k
ZeroDivisionError: integer division or modulo by zero
Run Code Online (Sandbox Code Playgroud)
如何在traceback simillar中包含变量值(h,k,...)到http://www.doughellmann.com/PyMOTW/cgitb/?当我包含cgitb结果是一样的.
编辑:
很好的答案我只是这样修改它所以它在文件中记录跟踪
def handleException(excType, excValue, trace):
cgitb.Hook(logdir=os.path.dirname(__file__),
display=False,
format='text')(excType, excValue, trace)
Run Code Online (Sandbox Code Playgroud)
通过查看源代码cgitb.py,您应该可以使用以下内容:
import sys
import traceback
import cgitb
def handleException(excType, excValue, trace):
print 'error'
cgitb.Hook(format="text")(excType, excValue, trace)
sys.excepthook = handleException
h = 1
k = 0
print h/k
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1451 次 |
| 最近记录: |