Python try/except:显示我的变量后显示错误原因
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4560288/
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
Python try/except: Showing the cause of the error after displaying my variables
提问by NealWalters
I'm not even sure what the right words are to search for. I want to display parts of the error object in an except block (similar to the err object in VBScript, which has Err.Number and Err.Description). For example, I want to show the values of my variables, then show the exact error. Clearly, I am causing a divided-by-zero error below, but how can I print that fact?
我什至不确定要搜索的正确词是什么。我想在一个 except 块中显示部分错误对象(类似于 VBScript 中的 err 对象,它具有 Err.Number 和 Err.Description)。例如,我想显示变量的值,然后显示确切的错误。显然,我在下面导致了一个被零除的错误,但我如何打印这个事实?
try:
x = 0
y = 1
z = y / x
z = z + 1
print "z=%d" % (z)
except:
print "Values at Exception: x=%d y=%d " % (x,y)
print "The error was on line ..."
print "The reason for the error was ..."
采纳答案by Corey Goldberg
try:
1 / 0
except Exception as e:
print(e)
回答by Ignacio Vazquez-Abrams
The string value of the exception objectwill give you the reason. The tracebackmodule will allow you access to the full traceback.
回答by Senthil Kumaran
In other words,
换句话说,
try:
1/0
except Exception as e:
print e
You can get the details in the manual pages linked by Ignacio in his response.
您可以在 Ignacio 的回复中链接的手册页中获取详细信息。
回答by sahhhm
If you're expecting a DivideByZero error, you can catch that particular error
如果您预计会出现 DivideByZero 错误,则可以捕获该特定错误
import traceback
try:
x = 5
y = 0
print x/y
except ZeroDivisionError:
print "Error Dividing %d/%d" % (x,y)
traceback.print_exc()
except:
print "A non-ZeroDivisionError occurred"
You can manually get the line number and other information by calling traceback.print_exc()
可以通过调用手动获取行号等信息 traceback.print_exc()
回答by CadentOrange
A better approach is to make use of the standard Python Logging module.
更好的方法是使用标准的Python Logging 模块。
import sys, traceback, logging
logging.basicConfig(level=logging.ERROR)
try:
x = 0
y = 1
z = y / x
z = z + 1
print "z=%d" % (z)
except:
logging.exception("Values at Exception: x=%d y=%d " % (x,y))
This produces the following output:
这会产生以下输出:
ERROR:root:Values at Exception: x=0 y=1
Traceback (most recent call last):
File "py_exceptions.py", line 8, in <module>
z = y / x
ZeroDivisionError: integer division or modulo by zero
The advantage of using the logging module is that you have access to all the fancy log handlers (syslog, email, rotating file log), which is handy if you want your exception to be logged to multiple destinations.
使用日志记录模块的优点是您可以访问所有花哨的日志处理程序(系统日志、电子邮件、旋转文件日志),如果您希望将异常记录到多个目的地,这将非常方便。
回答by grofte
If you do
如果你这样做
except AssertionError as error:
print(error)
then that should crash your program with trace and everything as if the try/except wasn't there. But without having to change indentation and commenting out lines.
那么这应该会使您的程序与跟踪和一切崩溃,就好像 try/except 不存在一样。但无需更改缩进和注释行。

