我经常在其他Stack Overflow问题上看到关于如何不鼓励使用except: pass的评论。这为什么不好呢?有时我不在乎错误是什么,我只想继续写代码。

try:
    something
except:
    pass

为什么使用except: pass块不好?是什么让它变得糟糕?是我传递了一个错误还是我排除了任何错误?


当前回答

我个人更喜欢这个解决方案:

except ValueError as error:
                print(error.args)
                pass

错误。Args给了我一个简单的代码行,它不会太分散人的注意力,但确实有助于代码审查,特别是如果错误有不同的原因,比如

(ValueError('year 0 is out of range'),)
(ValueError('month must be in 1..12'),)
(ValueError('day is out of range for month'),)

当研究熊猫的时间周期时。

其他回答

正如您所猜测的那样,它有两个方面:通过在except后面不指定异常类型来捕获任何错误,并简单地传递它而不采取任何操作。

我的解释“有点”长,所以可以分解成这样:

不要捕捉任何错误。始终指定准备从哪些异常中恢复,并只捕获这些异常。 尽量避免传入除块。除非有明确的愿望,否则这通常不是一个好迹象。

但让我们来详细谈谈:

不要捕捉任何错误

在使用try块时,通常会这样做,因为您知道有可能抛出异常。因此,您也已经大致了解了可以破坏什么以及可以抛出什么异常。在这种情况下,您可以捕获异常,因为您可以积极地从中恢复。这意味着您已经为异常做好了准备,并有了一些可选的计划,以便在出现异常的情况下执行。

For example, when you ask for the user to input a number, you can convert the input using int() which might raise a ValueError. You can easily recover that by simply asking the user to try it again, so catching the ValueError and prompting the user again would be an appropriate plan. A different example would be if you want to read some configuration from a file, and that file happens to not exist. Because it is a configuration file, you might have some default configuration as a fallback, so the file is not exactly necessary. So catching a FileNotFoundError and simply applying the default configuration would be a good plan here. Now in both these cases, we have a very specific exception we expect and have an equally specific plan to recover from it. As such, in each case, we explicitly only except that certain exception.

然而,如果我们要捕获所有的异常,那么除了我们准备从这些异常中恢复之外,我们也有可能得到我们没有预料到的异常,并且我们确实无法从中恢复;或者不应该恢复。

Let’s take the configuration file example from above. In case of a missing file, we just applied our default configuration and might decide at a later point to automatically save the configuration (so next time, the file exists). Now imagine we get a IsADirectoryError, or a PermissionError instead. In such cases, we probably do not want to continue; we could still apply our default configuration, but we later won’t be able to save the file. And it’s likely that the user meant to have a custom configuration too, so using the default values is likely not desired. So we would want to tell the user about it immediately, and probably abort the program execution too. But that’s not something we want to do somewhere deep within some small code part; this is something of application-level importance, so it should be handled at the top—so let the exception bubble up.

Python 2习语文档中还提到了另一个简单的例子。这里,代码中存在一个简单的拼写错误,导致代码中断。因为我们正在捕获每个异常,所以我们还捕获NameErrors和SyntaxErrors。这两种错误都是我们在编程过程中会遇到的,而且这两种错误都是我们在发布代码时绝对不希望出现的。但是因为我们也捕获了这些,我们甚至不知道它们发生在那里,并且失去了正确调试它的任何帮助。

但也有更危险的例外情况,我们不太可能做好准备。例如,SystemError通常很少发生,我们无法真正计划;这意味着有一些更复杂的事情正在发生,一些可能阻止我们继续当前任务的事情。

In any case, it’s very unlikely that you are prepared for everything in a small-scale part of the code, so that’s really where you should only catch those exceptions you are prepared for. Some people suggest to at least catch Exception as it won’t include things like SystemExit and KeyboardInterrupt which by design are to terminate your application, but I would argue that this is still far too unspecific. There is only one place where I personally accept catching Exception or just any exception, and that is in a single global application-level exception handler which has the single purpose to log any exception we were not prepared for. That way, we can still retain as much information about unexpected exceptions, which we then can use to extend our code to handle those explicitly (if we can recover from them) or—in case of a bug—to create test cases to make sure it won’t happen again. But of course, that only works if we only ever caught those exceptions we were already expecting, so the ones we didn’t expect will naturally bubble up.

尽量避免传入除块

当显式地捕获一小部分特定异常时,在许多情况下,我们什么都不做就可以了。在这种情况下,使用except SomeSpecificException: pass就可以了。但大多数情况下,情况并非如此,因为我们可能需要一些与恢复过程相关的代码(如上所述)。例如,这可以是再次重试操作,或者设置一个默认值。

如果不是这样,例如,因为我们的代码已经被构造为重复直到成功,那么仅仅传递就足够了。以上面的例子为例,我们可能想让用户输入一个数字。因为我们知道用户不喜欢做我们要求他们做的事情,我们可能会把它放在一个循环中,所以它看起来像这样:

def askForNumber ():
    while True:
        try:
            return int(input('Please enter a number: '))
        except ValueError:
            pass

因为我们一直尝试,直到没有抛出异常,所以我们不需要在except块中做任何特殊的事情,所以这很好。当然,有人可能会说,我们至少要向用户显示一些错误消息,告诉他为什么必须重复输入。

不过,在许多其他情况下,仅仅传入一个except是一个迹象,表明我们没有真正为正在捕获的异常做好准备。除非这些异常很简单(如ValueError或TypeError),并且可以传递的原因很明显,否则尽量避免直接传递。如果真的没什么可做的(并且你绝对确定),那么考虑添加一个注释为什么会这样;否则,展开except块以实际包含一些恢复代码。

除了:通过

The worst offender though is the combination of both. This means that we are willingly catching any error although we are absolutely not prepared for it and we also don’t do anything about it. You at least want to log the error and also likely reraise it to still terminate the application (it’s unlikely you can continue like normal after a MemoryError). Just passing though will not only keep the application somewhat alive (depending on where you catch of course), but also throw away all the information, making it impossible to discover the error—which is especially true if you are not the one discovering it.


因此,底线是:只捕获您真正期望并准备从中恢复的异常;所有其他的可能要么是你应该改正的错误,要么是你根本没有准备好的事情。如果您真的不需要对特定的异常做什么,那么传递特定的异常是可以的。在所有其他情况下,这只是一种傲慢和懒惰的表现。你肯定想解决这个问题。

第一个原因已经说过了——它隐藏了你没有预料到的错误。

(#2)——它使你的代码难以被其他人阅读和理解。如果你捕捉FileNotFoundException当你试图读取一个文件,那么它是相当明显的另一个开发人员'catch'块应该有什么功能。如果您没有指定异常,那么您需要额外的注释来解释该块应该做什么。

(#3) -它演示了惰性编程。如果您使用通用的try/catch,则表明您不了解程序中可能的运行时错误,或者您不知道Python中可能存在哪些异常。捕捉特定的错误表明您了解程序和Python抛出的错误范围。这更有可能使其他开发人员和代码审查人员信任您的工作。

In my opinion errors have a reason to appear, that my sound stupid, but thats the way it is. Good programming only raises errors when you have to handle them. Also, as i read some time ago, "the pass-Statement is a Statement that Shows code will be inserted later", so if you want to have an empty except-statement feel free to do so, but for a good program there will be a part missing. because you dont handle the things you should have. Appearing exceptions give you the chance to correct input data or to change your data structure so these exceptions dont occur again (but in most cases (Network-exceptions, General input-exceptions) exceptions indicate that the next parts of the program wont execute well. For example a NetworkException can indicate a broken network-connection and the program cant send/recieve data in the next program steps.

但是只对一个执行块使用pass块是有效的,因为你仍然可以区分不同类型的异常,所以如果你把所有的异常块放在一个中,它就不是空的:

try:
    #code here
except Error1:
    #exception handle1

except Error2:
    #exception handle2
#and so on

可以写成这样:

try:
    #code here
except BaseException as e:
    if isinstance(e, Error1):
        #exception handle1

    elif isinstance(e, Error2):
        #exception handle2

    ...

    else:
        raise

因此,即使是多个带有pass语句的异常块也可能导致代码,其结构处理特殊类型的异常。

>>> import this

The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!

所以,这是我的观点。每当你发现一个错误,你应该做一些事情来处理它,即把它写在日志文件或其他东西。至少,它告诉您曾经有一个错误。

通常,您可以将任何错误/异常分为以下三类之一:

Fatal: Not your fault, you cannot prevent them, you cannot recover from them. You should certainly not ignore them and continue, and leave your program in an unknown state. Just let the error terminate your program, there is nothing you can do. Boneheaded: Your own fault, most likely due to an oversight, bug or programming error. You should fix the bug. Again, you should most certainly not ignore and continue. Exogenous: You can expect these errors in exceptional situations, such as file not found or connection terminated. You should explicitly handle these errors, and only these.

在除了:pass之外的所有情况下,pass只会让你的程序处于未知状态,在这种状态下它会造成更多的损害。