在 Python 提示符下引发错误后,如何获取最后一个异常对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15605925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the last exception object after an error is raised at a Python prompt?
提问by Ben Hoyt
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 requestslibrary (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 中的代码通常很容易,但这并不理想。我还意识到,在这种特定情况下,如果我正在使用该requests库(它不会因 HTTP 错误而引发),我就不会遇到这个问题……但我真的想知道是否有更通用的方法来获取在这些情况下,Python 提示符处的最后一个异常对象。
采纳答案by Cairnarvon
The sysmodule 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_valueis the one you're looking for.
sys.last_value就是你要找的那个。
回答by Sambhav Pandey
As @Cairnarvon mentioned, I didn't find any last_valuemember is sys module.
正如@Cairnarvon 提到的,我没有发现任何last_value成员是 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()返回一个包含三个值的元组(type, value, traceback)。
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
会输出 list assignment index out of range
Also, traceback.format_exc()from tracebackmodule can be used to print out the similar information.
此外,可以使用traceback.format_exc()fromtraceback模块打印出类似的信息。
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

