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

try:
    something
except:
    pass

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


当前回答

正如您所猜测的那样,它有两个方面:通过在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.


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

其他回答

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语句的异常块也可能导致代码,其结构处理特殊类型的异常。

我正在构建一个将在数据中心运行的应用程序。它不应该生成任何错误或引发任何异常。我的数据中心有一个网络监控系统,其中包括一个SNMP trap接收器。

try:
    main()
except as e:
    log(str(e))
    send_snmp_trap(str(e))
    raise

但是这个加薪不会有任何效果因为它是和任何可能剩下的堆栈的底部。

顺便说一句,这不是万能的灵丹妙药。有一些例外情况是无法被发现的。SNMP不能保证传输。YMMV。

>>> 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!

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

首先,它违背了Python的两个禅宗原则:

显性比隐性好 错误绝不能悄无声息地过去

它的意思是,你故意让你的错误悄无声息地过去。此外,您不知道究竟发生了哪个错误,因为except: pass将捕获任何异常。

其次,如果我们试图从Python的禅意中抽象出来,而只是从理智的角度来说话,你应该知道,使用except:pass会让你在系统中失去知识和控制。经验法则是,如果发生错误,就引发异常,并采取适当的操作。如果你事先不知道这些操作应该是什么,至少在某个地方记录错误(最好重新引发异常):

try:
    something
except:
    logger.exception('Something happened')

但是,通常情况下,如果您试图捕获任何异常,那么您可能正在做错误的事情!

这里的主要问题是它忽略所有和任何错误:内存不足,CPU正在燃烧,用户想要停止,程序想要退出,Jabberwocky正在杀死用户。

这太过分了。在你的脑海中,你在想“我想忽略这个网络错误”。如果出现了意想不到的错误,那么您的代码将继续以完全不可预测的方式中断,没有人可以调试。

这就是为什么您应该限制自己只忽略一些错误,而让其余的错误过去。