Search code examples
rubyloopstwittererror-handlingrescue

Using Ruby, can you use more than one rescue in a begin loop?


Is it possible to add more than one rescue in a begin loop and/or a function and still have a next as well for each?

For example:

begin twitter_function

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

Solution

  • Yes, We can do next in begin - rescue loop. We can do this in following manner -

    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