本文翻译自:Python: How to ignore an exception and proceed? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- How to properly ignore exceptions 11 answers 如何正确忽略异常 11个答案
I have a try...except block in my code and When an exception is throw. 我有一个尝试...除了我的代码中的块和抛出异常时。 I really just want to continue with the code because in that case, everything is still able to run just fine. 我真的只想继续使用代码,因为在这种情况下,一切都仍然可以正常运行。 The problem is if you leave the except: block empty or with a #do nothing, it gives you a syntax error. 问题是如果你将except:block留空或者没有#do,它会给你一个语法错误。 I can't use continue because its not in a loop. 我不能继续使用,因为它不在循环中。 Is there a keyword i can use that tells the code to just keep going? 是否有一个关键字,我可以使用它告诉代码继续前进?
#1楼
参考:https://stackoom.com/question/2PVq/Python-如何忽略异常并继续-重复
#2楼
There's a new way to do this coming in Python 3.4: 在Python 3.4中有一种新方法可以做到这一点:
from contextlib import suppress
with suppress(Exception):
# your code
Here's the commit that added it: http://hg.python.org/cpython/rev/406b47c64480 这是添加它的提交: http : //hg.python.org/cpython/rev/406b47c64480
And here's the author, Raymond Hettinger, talking about this and all sorts of other Python hotness (relevant bit at 43:30): http://www.youtube.com/watch?v=OSGv2VnC0go 以下是作者Raymond Hettinger,谈论这个以及各种其他Python热点(相关位于43:30): http : //www.youtube.com/watch?v = OSGv2VnC0go
If you wanted to emulate the bare except
keyword and also ignore things like KeyboardInterrupt
—though you usually don't—you could use with suppress(BaseException)
. 如果你想模拟bare except
关键字,也忽略了KeyboardInterrupt
类的东西 - 虽然你通常不会 - 你可以使用with suppress(BaseException)
。
Edit: Looks like ignored
was renamed to suppress
before the 3.4 release. 编辑:在3.4版本之前,看起来像ignored
重命名为suppress
。
#3楼
except:
pass
Python docs for the pass statement 传递语句的Python文档
#4楼
Try this: 试试这个:
try:
blah()
except:
pass
#5楼
Generic answer 通用答案
The standard "nop" in Python is the pass
statement: Python中的标准“nop”是pass
语句:
try:
do_something()
except Exception:
pass
Using except Exception
instead of a bare except
avoid catching exceptions like SystemExit
, KeyboardInterrupt
etc. 使用except Exception
而不是bare, except
避免捕获SystemExit
, KeyboardInterrupt
等异常。
Python 2 Python 2
Because of the last thrown exception being remembered in Python 2, some of the objects involved in the exception-throwing statement are being kept live indefinitely (actually, until the next exception). 由于在Python 2中记住了最后抛出的异常,异常抛出语句中涉及的一些对象将无限期地保持活动(实际上,直到下一个异常)。 In case this is important for you and (typically) you don't need to remember the last thrown exception, you might want to do the following instead of pass
: 如果这对您很重要并且(通常)您不需要记住最后抛出的异常,您可能希望执行以下操作而不是pass
:
try:
do_something()
except Exception:
sys.exc_clear()
This clears the last thrown exception. 这清除了最后抛出的异常。
Python 3 Python 3
In Python 3, the variable that holds the exception instance gets deleted on exiting the except
block. 在Python 3中,保留异常实例的变量在退出except
块时被删除 。 Even if the variable held a value previously, after entering and exiting the except
block it becomes undefined again. 即使变量先前保持一个值,在进入和退出except
块之后它也会再次变为未定义 。