什么时候需要在try..except中添加`else`子句?

什么时候需要在try..except中添加`else`子句?,第1张

什么时候需要在try..except中添加`else`子句

实际上,在第二个片段中,最后一行总是执行。

你可能是说

try:    some_pre_that_can_cause_an_exception()    pre_that_needs_to_run_when_there_are_no_exceptions()except:    some_pre_to_handle_exceptions()

我相信

else
如果可以使代码更具可读性,则可以使用该版本。
else
如果您不想从中捕获异常,则可以使用变体
pre_that_needs_to_run_when_there_are_no_exceptions



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5655109.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存