Skip to content

Instantly share code, notes, and snippets.

@sstelfox
Last active August 29, 2015 14:01
Show Gist options
  • Save sstelfox/3e2c1b5125b7b3515282 to your computer and use it in GitHub Desktop.
Save sstelfox/3e2c1b5125b7b3515282 to your computer and use it in GitHub Desktop.
Just exploring how ensure behaves when an exception is raised within a rescue block, and how retry affects it all.
#!/usr/bin/env ruby
@crashes = 0
def exception_test(state)
puts "Main block: #{state}"
# Modify our 'state'
state += 1
raise 'Oh god, Im shot!'
rescue => e
puts e.message
puts 'Rescue!'
if @crashes < 3
@crashes += 1
puts 'Try again!'
retry
end
puts 'Sad day for a wizard...'
raise e
ensure
puts "Ensure called"
end
exception_test(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment