Python例外的最佳做法? [英] Best Practices for Python Exceptions?

查看:83
本文介绍了Python例外的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建例外的最佳实践是什么?我刚刚看到了这个,但我不知道我应该被吓坏还是喜欢。我在书中多次读到,异常永远不应该包含字符串,因为字符串本身可以引发异常。对此有任何真正的道理吗?

What are the best practices for creating exceptions? I just saw this, and I don't know if I should be horrified, or like it. I read several times in books that exceptions should never ever hold a string, because strings themselves can throw exceptions. Any real truth to this?

基本上,根据我对脚本的理解,这样做是为了使所有内部Python库都具有通用的错误消息格式(即迫切需要),这样我就能理解为什么放置错误消息字符串是一个好主意。 (由于完全不需要无效遍历,几乎每个方法都会引发异常。)

Basically from my understanding from the scripts is that this was done so all the inhouse Python libraries will have a common error message format (something that is desperately needed) so I can understand why putting the error message string is a good idea. (Almost every method throws exceptions due to the utter need for nothing invalid getting through).

所讨论的代码如下:

"""
Base Exception, Error
"""
class Error(Exception):
    def __init__(self, message):
        self.message = message

    def __str__(self):
        return "[ERROR] %s\n" % str(self.message)

    def log(self):
        ret = "%s" % str(self.message)
        if(hasattr(self, "reason")):
            return "".join([ret, "\n==> %s" % str(self.reason)])
        return ret

class PCSException(Error):
    def __init__(self, message, reason = None):
        self.message = message
        self.reason = reason
    def __str__(self):
        ret = "[PCS_ERROR] %s\n" % str(self.message)
        if(self.reason != None):
            ret += "[REASON] %s\n" % str(self.reason)
        return ret

这只是冰山一角,但有人能给我一些使这成为冰山一角的见解可怕的主意?或者,如果还有更好的异常编码过程/样式。

This is just the tip of the iceberg, but can someone give me some insight in what makes this a terrible idea? Or if there is a much better exception coding process/style.

推荐答案


我在
异常的书永远不要包含
字符串,因为字符串本身可以
抛出异常。
有任何真实的真相吗?

I read several times in books that exceptions should never ever hold a string, because strings themselves can throw exceptions. Any real truth to this?

什么?

请提供参考或链接。

由于所有对象都可以引发异常,因此该逻辑不能将任何对象包含在异常中。

Since all objects can throw exceptions, no object could be contained in an exception by that logic.

不,在Python上下文中,没有字符串简直是疯了。也许您是在C ++上下文中阅读它的。

No, the "no strings" is simply crazy in a Python context. Perhaps you read it in a C++ context.

编辑

从前(从前),您可以按名称而不是实际的类引发Python异常。

Once upon a time (back in the olden days) you could raise a Python exception by name instead of by the actual class.

raise "SomeNameOfAnExceptionClass"

这很糟糕。但这不是在异常内包含字符串。这是使用字符串而不是实际的类对象来命名异常。在2.5中,它仍然可以工作,但是会收到弃用警告。

This is bad. But this is not including a string inside an exception. This is naming the exception with a string instead of the actual class object. In 2.5, this can still work, but gets a deprecation warning.

也许这就是您读到的不要用字符串名引发异常

Perhaps this is what you read "Do not raise an exception with a string name"

这篇关于Python例外的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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