Last active
August 29, 2015 14:01
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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