捕获不继承自Exception的异常 [英] Catching exceptions that don't inherit from Exception

查看:117
本文介绍了捕获不继承自Exception的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用查询或命令调用的数据库访问模块。它找出如何处理数据库,并尝试执行该操作。但是,例如,如果查询或命令字符串是病态的,则对基础PGDB模块的调用可能会引发异常。

I have a database access module that I call with a query or a command. It figures out what to do with the database, and tries to do it. But, for instance if the query or command string is pathological, the call to the underlying PGDB module can throw an exception.

PGDB返回了一些非常有用的信息(来自PostgreSQL),专门指出在查询或命令中发现了什么错误。 PGD​​B各种功能的这种用法可检索该信息:

Some incredibly useful information is returned by PGDB (from PostgreSQL under that), specifically calling out what errors were found in the query or command. This kind of usage of PGDB's various functions retrieves that information:

try:
    pgdb.dothing(mod.withx)
except Exception, e:
    mod.error = 'pgdb.dothing('+str(type(mod.withx))+str(mod.withx)+') failed with: '+str(e)

然后,当类返回失败时,对象包含.error和viola中的消息,我可以在查询或命令中解决我的愚蠢问题。

Then when the class returns, having failed, the object contains the message in .error and viola, I can fix my stupidity in the query or command.

这一切似乎都可以正常工作-(在Python 2.2.2中,可能会更改为2。某天更高...但不是现在-永远都不会达到3.任何水平)

And this all seems to work just fine -- (in Python 2.2.2, which might change to 2.higher someday... but not now -- and never, ever to 3.whatever)

但是...我发现了这种不透明性: 异常不必从Exception继承。因此普通的'except:'捕获所有异常,不仅是系统异常。字符串异常是不从Exception继承的异常的一个示例

But... I have found this bit of opacity: "exception doesn't have to be inherited from Exception. Thus plain 'except:' catches all exceptions, not only system. String exceptions are one example of an exception that doesn't inherit from Exception"

所以这是问题:我为什么在乎?如果引发异常,我想知道为什么。我不在乎它的来源,真的,我只想要错误消息,并且我当然不希望Python崩溃。这将包括错误是否来自字符串事物或其他原因。因此,除了抓到所有东西都是好事。

So here's the question: Why do I care? If an exception is thrown, I want to know why. I don't care where it came from, really, I just want the error message, and I sure don't want Python to come to a crashing halt. That would include if the error came from the string thing or whatever. So except catching everything is good. Or it should be.

是Exception参数的意思是,如果它来自String Innards,那么我不会捕获错误?然后,代码将因未捕获的异常而暂停?然后,对于不从异常继承的每种类型,我都需要进行一系列的捕获才能获得我想要的行为?像这样的东西:

Is it that the parameter of Exception means, I won't catch an error if it comes from, for instance, the String innards? And that then, the code will halt with an uncaught exception? And that then I'd need a series of catches for every type that "doesn't inherit from exception" in order to get the behavior I want? Something like this:

try:
    pgdb.dothing(mod.withx)
except Exception, e:
    mod.error = 'pgdb.dothing('+str(type(mod.withx))+str(mod.withx)+') failed with: '+str(e)
except:
    mod.error = 'pgdb.dothing('+str(type(mod.withx))+str(mod.withx)+') failed with: WTF???'

...因为确实...有点烂。

...because that really... kinda sucks.

如果是这样,我还有其他方法可以捕获所有类型的所有异常并获取它们的错误消息吗?

And, if that's the case, is there some other way I can catch all exceptions of all types and grab the error message for them? It seems like this would be something that is highly, highly desirable (and it also seems like a one-liner should solve it, and it should look somewhat like the former example, rather than the latter.)

请在回答之前:是的,我知道Python 2.2.2很旧。不,它不会很快升级。这是一个具有数百万行代码的生产系统;它很稳定,我们希望它保持这种状态,基于不损坏,不修复。

Please, before you answer: Yes, I know Python 2.2.2 is old. No, it's not going to be upgraded soon. This is a production system with several million lines of code; it's stable and we want it to remain that way, based upon, "not broken, not fixing."

我只需要对异常过程的这一部分有深入的了解殴打我所有的解释似乎都是对我所知道的是乐观的假设。 :)

I just need a solid understanding of this part of the exception process beaten into me. All the explanations seem to be making assumptions about what I know that are... optimistic. :)

感谢您提供任何见识。

推荐答案

尝试一下,尽管这是一个坏习惯。覆盖错误时,您应该尽可能明确。

Try this, although this is a bad practice. You should be as explicit as possible when overriding errors.

try:
    ...
except:
    e = sys.exc_info()
    print e

我也建议您看看 https://wiki.python.org/moin/HandlingExceptions

这篇关于捕获不继承自Exception的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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