python '除了例外:'的pylint警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/756180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pylint warning on 'except Exception:'
提问by yanchenko
For a block like this:
对于这样的块:
try:
#some stuff
except Exception:
pass
pylint raises warning W0703 'Catch "Exception"'. Why?
pylint 引发警告 W0703 'Catch "Exception"'。为什么?
回答by Greg
It's considered good practice to not normally catch the root Exception object, instead of catching more specific ones - for example IOException.
通常不捕获根异常对象,而不是捕获更具体的对象 - 例如 IOException,被认为是一种很好的做法。
Consider if an out of memory exception occurred - simply using "pass" isn't going to leave your programme in a good state.
考虑是否发生内存不足异常 - 仅使用“pass”不会使您的程序处于良好状态。
Pretty much the only time you should catch Exception is at the top level of your programme, where you can (try to) log it, display an error, and exit as gracefully as you can.
几乎唯一应该捕获异常的时间是在程序的顶层,在那里您可以(尝试)记录它,显示错误,并尽可能优雅地退出。
回答by Jonathan Hartley
It's good practice to catch only a very narrow range of types. 'Exception' is too general - you will end up catching not just the errors you planned for, but other errors too, which may mask bugs in your code that would be quicker to diagnose if they weren't caught at all, or possibly would be better dealt with by a single very high level exception handler.
仅捕获范围非常窄的类型是一种很好的做法。“异常”太笼统了——你最终不仅会捕捉到你计划的错误,还会捕捉到其他错误,这可能会掩盖你的代码中的错误,如果它们根本没有被捕捉到,这些错误会更快地诊断出来,或者可能会最好由一个非常高级的异常处理程序处理。
Having said that, since Python2.6, catching Exception has become a lot more reasonable, because all the exceptions that you wouldn't want to catch (SystemExit, KeyboardInterrupt) no longer inherit from Exception. They instead inherit from a common BaseException instead. This has been done deliberately in order to make catching Exception relatively harmless, since it is such a common idiom.
话虽如此,从Python2.6开始,捕获Exception变得更加合理,因为所有你不想捕获的异常(SystemExit、KeyboardInterrupt)都不再继承自Exception。相反,它们是从公共 BaseException 继承的。这是故意这样做的,目的是使捕获异常相对无害,因为它是如此常见的习语。
See PEP 3110for details & future plans.
有关详细信息和未来计划,请参阅PEP 3110。
回答by SilentGhost
because it thinks that you're catching too much. and it's right.
因为它认为你抓得太多了。这是正确的。
回答by Bastien Léonard
Exception are raised when something... exceptional occurs. It's generally a good thing that the program terminates.
当某些事情发生时会引发异常......异常发生。程序终止通常是一件好事。
You may want to ignore some exceptions, but IMO there's no good reason for catching a base-class like that.
您可能想忽略一些异常,但 IMO 没有充分的理由来捕获这样的基类。
回答by Remi
like Greg's answer, 'Exception' is a base class and exceptions should be derived from this class, see also exceptions.Exception.
像 Greg 的回答一样,'Exception' 是一个基类,异常应该从这个类派生,另见exceptions.Exception。
Here a very usefull list of Errors in pydocs
这里有一个非常有用的pydocs错误列表
Note also the very handy traceback module which allows you to find out where the exception occured. Using only 'except: ...' will show you what Error you should best use in your case. For example, try this code (toggle the comment), perhaps you'll accept it:
还要注意非常方便的回溯模块,它允许您找出发生异常的位置。仅使用 'except: ...' 将向您显示在您的情况下最好使用的 Error。例如,试试这个代码(切换评论),也许你会接受它:
import traceback
#absent = 'nothing'
try:
something = absent
except NameError:
traceback.print_exc()
else:
print("you get here only when you uncomment 'absent'")
回答by Remi
Catching Exception (without re-raising) has 2 really bad side effects: errors get eaten, so you lose the stack trace, but also that ctrl-c (or whatever the break key is on your operating system) also gets handled here.
捕获异常(无需重新引发)有 2 个非常糟糕的副作用:错误被吃掉,因此您会丢失堆栈跟踪,而且 ctrl-c(或操作系统上的任何中断键)也在这里得到处理。
The typical behavior of programs like this is that either they can't be stopped, or that ctrl-c causes the control flow to skip forward (to the exception handler), and then continue. Then either the code can't be interrupted, or you need to hammer on ctrl-c to get it to stop.
像这样的程序的典型行为是它们不能被停止,或者 ctrl-c 导致控制流向前跳过(到异常处理程序),然后继续。那么要么代码不能被中断,要么你需要敲 ctrl-c 来让它停止。