Ruby Language Reference

retry statement

Syntax

The control flow jumps to the top of the begin-end block when retry is called from within a rescue clause.

def test
  attempt_again = true
  p 'Testing'
  begin
    # This is the point where the control flow jumps
    p 'About to crash'
    raise 'Boom'
  rescue Exception => e
    if attempt_again
      attempt_again = false
      retry       
    end
  end
end

test

This will print:

Testing
About to crash
About to crash

In Ruby 2.3.1, you will get:

Invalid retry (SyntaxError)

when you use retry outside a rescue block.


Sharing is Caring