建议更好的处理 Python 异常的方法


我们可以使用 finally 子句来清理是否抛出异常

try:
  #some code here
except:
  handle_exception()
finally:
  do_cleanup()

如果在异常事件中进行清理,我们可以按如下方式编码

should_cleanup = True
try:
  #some code here
  should_cleanup = False
except:
  handle_exception()
finally:
  if should_cleanup():
    do_cleanup()

更新时间:2019 年 9 月 27 日

137 次浏览

开启 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.