擅长:python、mysql、java
<p>我喜欢追踪模块。</p>
<p>您可以使用<code>sys.exc_info()</code>获取回溯对象。然后,可以使用该对象使用<code>traceback.extract_tb()</code>获取一个经过预处理的回溯条目列表。然后您可以使用<code>traceback.format_list()</code>获得一个可读列表,如下所示:</p>
<pre><code>import sys
import traceback, inspect
try:
f = open("nonExistant file",'r')
except:
(exc_type, exc_value, exc_traceback) = sys.exc_info()
#print exception type
print exc_type
tb_list = traceback.extract_tb(sys.exc_info()[2])
tb_list = traceback.format_list(tb_list)
for elt in tb_list:
print elt
#Do any processing you need here.
</code></pre>
<p>请参阅sys模块:<a href="http://docs.python.org/library/sys.html" rel="nofollow">http://docs.python.org/library/sys.html</a></p>
<p>回溯模块:<a href="http://docs.python.org/library/traceback.html" rel="nofollow">http://docs.python.org/library/traceback.html</a></p>