Python 尝试/除 : Showing the cause of the error after displaying my variables

标签 python exception-handling

我什至不确定要搜索什么合适的词。我想在 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/

相关文章:

python - GIMP Python 插件加载 2 个图像作为图层

python - 根据值在字典列表中查找相似项目

c# - try/catch/finally 中 "finally"的用途是什么

安卓。让开发人员知道抛出的异常的最佳方式

asp.net-mvc - 如何在 ASP.NET MVC 3 应用程序中处理未捕获的异常?

python - QSortFilterProxyModel 创建空白项

python - 从 BS4 中提取并存储为 Python 中的列表元素

python - 在一行中创建和扩展列表

php - 自定义异常的 Laravel 5.1 自定义参数

c# - unity 注册实例并解析