我什至不确定要搜索什么合适的词。我想在 except block 中显示部分错误对象(类似于 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 ..."
最佳答案
try:
1 / 0
except Exception as e:
print(e)
关于Python 尝试/除 : Showing the cause of the error after displaying my variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4560288/