Python try / except:显示我的变量后出现错误的原因 [英] Python try/except: Showing the cause of the error after displaying my variables

查看:483
本文介绍了Python try / except:显示我的变量后出现错误的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定正确的话要搜索什么。我想在一个except块中显示错误对象的部分(类似于VBScript中的err对象,它具有Err.Number和Err.Description)。例如,我想显示我的变量的值,然后显示确切的错误。显然,我在下面引起了一个除零的错误,但是我怎么能打印出这个事实?

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆