处理任意异常,打印默认异常消息 [英] Handle Arbitrary Exception, Print Default Exception Message

查看:177
本文介绍了处理任意异常,打印默认异常消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,其中一部分执行一个循环。在执行此循环期间,有例外。显然,我希望我的程序运行没有错误,但是为了进步,我希望程序在整个输入上执行,并且在抛出异常时不停止。执行此操作的最简单的方法是实现一个,除了块。



但是,当我这样做时,它除了的所有异常,并继续与程序,我从来没有得到看到异常消息,我需要调试。



有没有办法除了任何异常并且能够打印块之外的异常消息。

解决方案

pre> 尝试:
#stuff
除了例外为e:
打印e

traceback 模块提供了从异常对象提取更多信息的各种功能( e ,以上)。



错误和例外


I have a program, a part of which executes a loop. During the execution of this loop, there are exceptions. Obviously, I would like my program to run without errors, but for the sake of progress, I would like the program to execute over the entire input and not stop when an exception is thrown. The easiest way to do this would be by implementing an except block.

However, when I do this, it excepts all exceptions and continues with the program and I never get to see the exception message, which I need in order to debug.

Is there a way to except any arbitrary exception and be able to print out the exception message in the except block?

解决方案

try:
    #stuff
except Exception as e:
    print e

The traceback module provides various functions for extracting more information from the exception object (e, above).

Source Errors and Exceptions

这篇关于处理任意异常,打印默认异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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