Python:如何忽略异常并继续? [重复]

本文翻译自:Python: How to ignore an exception and proceed? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

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避免捕获SystemExitKeyboardInterrupt等异常。

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块之后它也会再次变为未定义

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值