Skip to content

Instantly share code, notes, and snippets.

@movstox
Last active November 1, 2019 22:39
Show Gist options
  • Save movstox/46b7f5011b61a9d068d23114865af946 to your computer and use it in GitHub Desktop.
Save movstox/46b7f5011b61a9d068d23114865af946 to your computer and use it in GitHub Desktop.
catch Exception and retry x times
def with_catch_and_retry(*exceptions_to_catch, times, &block)
block.call
rescue *exceptions_to_catch => e
(times -= 1).zero? ? raise(e) : retry
end
context = 'nice'
with_catch_and_retry(Exception, RuntimeError, 3) {
p "123: #{context}"
raise 'something'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment