如何在Python中编写一个空的缩进块?
我有一些代码,例如:
try:
do_the_first_part()
except SomeError:
do_the_next_part()
我收到一个错误,上面写着“预期有一个缩进块” - 但我不想在我的 except
块中写入任何内容,我只是希望它捕获并吞下异常。我怎样才能在Python中做到这一点?
另请参阅: 如何使用“pass”语句?
I have some code like:
try:
do_the_first_part()
except SomeError:
do_the_next_part()
I get an error that says "expected an indented block" - but I don't want to write anything inside my except
block, I just want it to catch and swallow the exception. How can I do this in Python?
See also: How to use "pass" statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编写即可
只需按照编辑中的方式
:@swillden 提出了一个很好的观点,即,总的来说,这是一个糟糕的想法。您至少应该说出
您想要处理的任何类型的错误。否则你可能会掩盖更大的问题。
Just write
as in
EDIT: @swillden brings up a good point, viz., this is a terrible idea in general. You should, at the least, say
or whatever kinds of errors you want to handle. Otherwise you can mask bigger problems.
对于那些非常不清楚为什么要这样做的人。这是一个例子,我最初认为空块是个好主意:
但重新组织语句会更清晰:
For those who are very unclear as to why you would want to do this. Here is an example where I initially thought that an empty block would be a good idea:
But it would be more clear to reorganize the statement:
我从来没有在更永久的代码中这样做过,但我经常将其作为占位符,
只需在其中粘贴 True 即可停止错误。不确定这是否有什么不好的地方。
I've never done this in more permanent code, but I frequently do it as a placeholder
Just sticking a True in there will stop the error. Not sure if there's anything bad about this.