在pdb中获取最后一个异常 [英] Get last exception in pdb

查看:305
本文介绍了在pdb中获取最后一个异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查最后一个例外在pdb /进入pdb之前? (使用python 2.7.5)。

Is there a way to examine the last exception when in pdb/before entering pdb? (Using python 2.7.5).

在我的代码中引发异常后,立即(是的,我没有输入任何其他命令) ,我做 sys.exc_info();这只是导致(无,无,无)。在这一点上,我可以执行 pdb.pm(),pdb从引发异常的点开始。

Immediately (yes, I enter no other commands at all) after an exception being raised in my code, I do sys.exc_info(); this just results in (None, None, None). At this point, I can do pdb.pm(), and pdb starts at the point that the exception is raised.

我想要检查这个异常对象(它不会被保存在变量中,然后再被提出)。

I'd like to be able to examine this exception object (it is not stored in a variable before being raised).

http://docs.python.org/2/library/pdb中没有任何明显的帮助.html http://docs.python.org/2/library/sys.html

编辑:我知道 set_trace 。我想在修改代码之前检查异常。

I know about set_trace. I'd like to examine the exception before I modify the code.

推荐答案

您可以使用 sys.last_value

You can use sys.last_value:

>>> no_such_var
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'no_such_var' is not defined
>>> import sys
>>> sys.last_value
NameError("name 'no_such_var' is not defined",)
>>> sys.last_value.args
("name 'no_such_var' is not defined",)







>>> no_such_var
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'no_such_var' is not defined
>>> import pdb, sys
>>> pdb.set_trace()
--Return--
> <stdin>(1)<module>()->None
(Pdb) sys.last_value
NameError("name 'no_such_var' is not defined",)

这篇关于在pdb中获取最后一个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆