如果 Python 遇到错误,它就会生成一些错误信息,称为“反向跟踪”。反向跟踪包含了出错消息、导致该错误的代码行号,以及导致该错误的函数调用的序列。这个序列称为“调用栈”。在 IDLE 中打开一个新的文件编辑器窗口,输入以下程序,并保存为 errorExample.py:
def spam():
bacon()
def bacon():
raise Exception('This is the error message.')
spam()
如果运行 errorExample.py,输出看起来像这样:
Traceback (most recent call last):
File "errorExample.py", line 7, in <module>
spam()
File "errorExample.py", line 2, in spam
bacon()
File "errorExample.py", line 5, in bacon
raise Exception('This is the error message.')
Exception: This is the error message.
通过反向跟踪,可以看到该错误发生在第 5 行,在 bacon() 函数中。这次特定的bacon() 调用来自第 2 行,在 spam() 函数中,它又在第 7 行被调用的。在从多个位置调用函数的程序中,调用栈就能帮助你确定哪次调用导致了错误。只要抛出的异常没有被处理,Python 就会显示反向跟踪。但你也可以调用traceback.format_exc(),得到它的字符串形式。如果你希望得到异常的反向跟踪的信息,但也希望 except 语句优雅地处理该异常,这个函数就很有用。在调用该函数之前,需要导入 Python 的 traceback 模块。例如,不是让程序在异常发生时就崩溃,可以将反向跟踪信息写入一个日志文件,并让程序继续运行。稍后,在准备调试程序时,可以检查该日志文件。在交互式环境中输入以下代码:
>>> import traceback
>>> try:
... raise Exception('This is the error message.')
...except:
... errorFile = open('errorInfo.txt', 'w')
... errorFile.write(traceback.format_exc())
... errorFile.close()
... print('The traceback info was written to errorInfo.txt.')
116
The traceback info was written to errorInfo.txt.
write() 方法的返回值是 116,因为 116 个字符被写入到文件中。反向跟踪文本被写入 errorInfo.txt。
Traceback (most recent call last):
File "<pyshell#28>", line 2, in <module>
Exception: This is the error message.