【问题标题】:Try except finally - Is EMPTY try except finally valid in python?Try except finally - EMPTY try except finally 在 python 中有效吗?
【发布时间】:2018-03-19 03:54:57
【问题描述】:

除了finally在python中阻塞之外可以空尝试吗,像这样,

try:
    #nothing here,
except:
    #nothing here as well,
finally:
    #nothing here too.

【问题讨论】:

  • 运行你的(无意义的)代码。
  • 您的块并不完全是空的——每个块都有注释。你的意思是完全空的,只包含 cmets,只包含 pass 语句,还是别的什么?为什么不尝试一下,看看会发生什么?
  • @RoryDaulton 解析器忽略 cmets;它们不能充当至少需要一个的语句。

标签: python python-3.x exception exception-handling


【解决方案1】:

当然,你可以使用pass 来传递一个块:

try:
    pass
except:
    pass
finally:
    pass

更多示例:

def a():
    pass

while True:
    pass

for i in range(10):
    pass
...

【讨论】:

  • 时间实际上是在while循环中过去的。
猜你喜欢
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
  • 2023-04-01
相关资源
最近更新 更多