Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 1.36 KB

retry-a-block-after-an-exception.md

File metadata and controls

50 lines (36 loc) · 1.36 KB

Retry A Block After An Exception

Ruby comes with a retry mechanism that allows you to recover from known exceptions by retrying the code that led to the exception. In network or timing-based situations where race conditions are possible, the most straightforward recourse may be to just retry a couple times.

Set up a begin / rescue block like you'd normally do for a chunk of code that may raise an exception. Then add a retry call to the rescue block.

begin
  puts "About to do a thing (#{retries})"

  raise StandardError if rand(5) != 4

  puts "Success!"
rescue StandardError => e
  retry
end

If an exception is raised, this will tell Ruby to re-execute the code in the begin block over and over until the exception isn't raised.

To avoid an infinite loop, you can limit the retries with a counting variable.

begin
  retries ||= 0
  puts "About to do a thing (#{retries})"

  raise StandardError if rand(5) != 4

  puts "Success!"
rescue StandardError => e
  retry if (retries += 1) < 3

  # all retries failed, re-raise exception
  raise e
end

This will re-raise after 3 tries.

Here is the full example

source