如何正确忽略异常 [英] How to properly ignore exceptions
本文介绍了如何正确忽略异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当您只想执行try-except但不处理异常时,如何在Python中完成呢?
When you just want to do a try-except without handling the exception, how do you do it in Python?
以下是正确的处理方式吗?
Is the following the right way to do it?
try:
shutil.rmtree(path)
except:
pass
推荐答案
try:
doSomething()
except:
pass
或
try:
doSomething()
except Exception:
pass
不同之处在于第一个也将捕获 KeyboardInterrupt
, SystemExit
之类的东西,它们直接从 exceptions.BaseException
而不是 exceptions.Exception $ c派生$ c>。
The difference is that the first one will also catch KeyboardInterrupt
, SystemExit
and stuff like that, which are derived directly from exceptions.BaseException
, not exceptions.Exception
.
有关详细信息,请参见文档:
See documentation for details:
try
statement- exceptions
这篇关于如何正确忽略异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文