[英]How to get the last exception object after an error is raised at a Python prompt?
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.通常,Python 打印出来的回溯和错误消息是不够的。 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.例如,在获取 URL 时,服务器可能会返回 40x 错误,而您需要通过
error.read()
获得响应的内容……但您已经没有错误对象了。 For example:例如:
>>> 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...
它可能包含有价值的错误信息......
I realize it's usually easy to re-run your code wrapped in a try/except, but that's not ideal.我意识到重新运行包含在 try/except 中的代码通常很容易,但这并不理想。 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.我也意识到,在这种特定情况下,如果我使用
requests
库(不会引发 HTTP 错误),我就不会遇到这个问题......但我真的想知道是否有更通用的方法来获取在这些情况下,Python 提示符处的最后一个异常对象。
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
是您正在寻找的那个。
As @Cairnarvon mentioned, I didn't find any last_value
member is sys module.正如@Cairnarvon 提到的,我没有发现任何
last_value
成员是 sys 模块。
sys.exc_info()
did the trick for me. sys.exc_info()
为我解决了问题。 sys.exc_info()
returns a tuple with three values (type, value, traceback)
. sys.exc_info()
返回一个包含三个值(type, value, traceback)
的元组。
So sys.exc_info()[1]
will give the readable error.所以
sys.exc_info()[1]
会给出可读错误。 Here is the example,这是示例,
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
将输出
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 format_exec()
is used,下面是使用
format_exec()
时的输出,
Traceback (most recent call last):
File "python", line 6, in <module>
IndexError: list assignment index out of range
The sys.last_type
, sys.last_value
, and sys.last_traceback
variables are not always defined. sys.last_type
、 sys.last_value
和sys.last_traceback
变量并不总是被定义。 Actually, they are intended to be used in an interactive session.实际上,它们旨在用于交互式会话。 I was able to get the last raised exception using
sys.exc_info()
.我能够使用
sys.exc_info()
获得最后一个引发的异常。 Usage:用法:
import sys
ex_type, ex_value, traceback = sys.exc_info()
The last raised exception object will be stored in ex_value
最后引发的异常对象将存储在
ex_value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.