加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

200 200 200 200 200 -40 200 200 -40 200 200 -40 200 200 200

发布时间:2020-12-16 22:37:48 所属栏目:百科 来源:网络整理
导读:考虑这个开始抢救 – 确保块: attempts=0begin make_service_call()rescue Exception retry unless attempts2 exit -1ensure attemps += 1end 如果您运行该代码,它会引发异常,因为没有名为’make_service_call()’的函数.所以,它重试.但是它将被困在无限循
考虑这个开始抢救 – 确保块:
attempts=0
begin
  make_service_call()
rescue Exception
  retry unless attempts>2
  exit -1
ensure
  attemps += 1
end

如果您运行该代码,它会引发异常,因为没有名为’make_service_call()’的函数.所以,它重试.但是它将被困在无限循环中,因为控制不会因为“重试”而“保证”.不应该’确保’部分的块确保其中的代码被执行无论在“开始”或“救援”发生什么?

当然,我可以在“开始”中增加计数 – 这不是重点.我只是提出一个关于“确定”的问题来澄清.

解决方法

确保部分在离开begin语句(以任何方式)执行时,但是当您重试时,您只是在语句内移动,以便确保部分不会被执行.

尝试此版本的示例来更好地了解发生了什么:

attempts = 0
begin
  make_service_call()
rescue Exception
  attempts += 1
  retry unless attempts > 2
  exit -1
ensure
  puts "ensure! #{attempts}"
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读