Created
December 2, 2014 15:30
-
-
Save fantgeass/3399cddac58eef3213eb to your computer and use it in GitHub Desktop.
how-to: exceptions in ruby
This file contains 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
def foo | |
yield | |
rescue | |
puts "Only on error" | |
else | |
puts "Only on success" | |
ensure | |
puts "Always executed" | |
end | |
foo{ raise "Error" } | |
puts "---" | |
foo{ "No error" } | |
# Output: | |
# Only on error | |
# Always executed | |
# --- | |
# Only on success | |
# Always executed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment