【问题标题】:Using Ruby, can you use more than one rescue in a begin loop?使用 Ruby,您可以在开始循环中使用多个救援吗?
【发布时间】:2015-08-10 07:56:51
【问题描述】:

是否可以在 begin 循环和/或函数中添加多个救援,并且每个救援仍然有下一个?

例如:

begin twitter_function

rescue Twitter::Error::RateLimit => error
  next
rescue Twitter::Error::Unauthorized => error
  next
end

【问题讨论】:

  • @HunterMcMillen 现在就试试,只是要让错误发生...

标签: ruby loops twitter error-handling rescue


【解决方案1】:

是的,我们可以在begin - rescue 循环中执行下一步。我们可以通过以下方式做到这一点 -

for i in 1..10
  begin
    do_something_that_might_raise_exceptions(i)
  raise ExpectedError1 => error1
    next # do_something_* again, with the next i
  raise ExpectedError2 => error2
    next # do_something_* again, with the next i
  end
end

【讨论】:

  • 我们可以在一开始就进行多个救援吗?
  • 是的,我们可以在开始循环中进行不止一次救援。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多