When debugging Python code at the interactive prompt (REPL), often I'll write some code which raises an exception, but I haven't wrapped it in a try
/except
, so once the error is raised, I've forever lost the exception object.
在交互式提示符(REPL)上调试Python代码时,我经常会编写一些引发异常的代码,但是我没有把它包装在try / except中,所以一旦出现错误,我就永远失去了异常对象。
Often the traceback and error message Python prints out isn't enough. For example, when fetching a URL, the server might return a 40x error, and you need the content of the response via error.read()
... but you haven't got the error object anymore. For example:
通常Python打印出来的追溯和错误消息是不够的。例如,在获取URL时,服务器可能会返回40x错误,并且您需要通过error.read()进行响应的内容...但是您还没有得到错误对象。例如:
>>> import urllib2
>>> f = urllib2.urlopen('http://example.com/api/?foo=bad-query-string')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
...
urllib2.HTTPError: HTTP Error 400: Bad Request
Drat, what did the body of the response say? It probably had valuable error information in it...
Drat,回应的主体说了什么?它可能包含有价值的错误信息......
I realize it's usually easy to re-run your code wrapped in a try/except, but that's not ideal. I also realize that in this specific case if I were using the requests
library (which doesn't raise for HTTP errors), I wouldn't have this problem ... but I'm really wondering if there's a more general way to get the last exception object at a Python prompt in these cases.
我意识到重新运行包含在try / except中的代码通常很容易,但这并不理想。我也意识到在这种特殊情况下,如果我使用请求库(不会引起HTTP错误),我就不会有这个问题...但我真的想知道是否有更通用的方法来获取在这些情况下,Python提示符下的最后一个异常对象。
2 个解决方案
#1
50
The sys
module provides some functions for post-hoc examining of exceptions: sys.last_type
, sys.last_value
, and sys.last_traceback
.
sys模块提供了一些用于事后检查异常的函数:sys.last_type,sys.last_value和sys.last_traceback。
sys.last_value
is the one you're looking for.
sys.last_value是您正在寻找的。
#2
8
As @Cairnarvon mentioned, I didn't find any last_value
member is sys module.
正如@Cairnarvon所提到的,我没有发现任何last_valuemember是sys模块。
sys.exc_info()
did the trick for me. sys.exc_info()
returns a tuple with three values (type, value, traceback)
.
sys.exc_info()为我做了伎俩。 sys.exc_info()返回一个包含三个值(类型,值,跟踪)的元组。
So sys.exc_info()[1]
will give the readable error. Here is the example,
所以sys.exc_info()[1]会给出可读错误。这是一个例子,
import sys
list = [1,2,3,4]
try:
del list[8]
except Exception:
print(sys.exc_info()[1])
will output list assignment index out of range
将输出列表分配索引超出范围
Also, traceback.format_exc()
from traceback
module can be used to print out the similar information.
此外,traceback模块中的traceback.format_exc()可用于打印出类似的信息。
Below is the output if fortmat_exec()
is used,
如果使用fortmat_exec(),下面是输出,
Traceback (most recent call last): File "python", line 6, in <module> IndexError: list assignment index out of range
Traceback(最近一次调用最后一次):文件“python”,第6行,在
#1
50
The sys
module provides some functions for post-hoc examining of exceptions: sys.last_type
, sys.last_value
, and sys.last_traceback
.
sys模块提供了一些用于事后检查异常的函数:sys.last_type,sys.last_value和sys.last_traceback。
sys.last_value
is the one you're looking for.
sys.last_value是您正在寻找的。
#2
8
As @Cairnarvon mentioned, I didn't find any last_value
member is sys module.
正如@Cairnarvon所提到的,我没有发现任何last_valuemember是sys模块。
sys.exc_info()
did the trick for me. sys.exc_info()
returns a tuple with three values (type, value, traceback)
.
sys.exc_info()为我做了伎俩。 sys.exc_info()返回一个包含三个值(类型,值,跟踪)的元组。
So sys.exc_info()[1]
will give the readable error. Here is the example,
所以sys.exc_info()[1]会给出可读错误。这是一个例子,
import sys
list = [1,2,3,4]
try:
del list[8]
except Exception:
print(sys.exc_info()[1])
will output list assignment index out of range
将输出列表分配索引超出范围
Also, traceback.format_exc()
from traceback
module can be used to print out the similar information.
此外,traceback模块中的traceback.format_exc()可用于打印出类似的信息。
Below is the output if fortmat_exec()
is used,
如果使用fortmat_exec(),下面是输出,
Traceback (most recent call last): File "python", line 6, in <module> IndexError: list assignment index out of range
Traceback(最近一次调用最后一次):文件“python”,第6行,在