Python try / except:显示我的变量后出现错误的原因 [英] Python try/except: Showing the cause of the error after displaying my variables
本文介绍了Python try / except:显示我的变量后出现错误的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
try:
x = 0
y = 1
z = y / x
z = z + 1
printz =%d%(z)
除了:
print异常值:x =%dy =%d%(x,y)
print错误在线...
print错误的原因是...
解决方案
try:
1/0
除了例外为e:
打印e
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?
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 try / except:显示我的变量后出现错误的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文