Created
August 8, 2017 18:04
-
-
Save Iristyle/a1b291f11cc853ab44aeea531462158b to your computer and use it in GitHub Desktop.
Ruby error handling approaches
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 | |
raise ArgumentError.new("bad arg yo") | |
end | |
def bar | |
begin | |
foo | |
# good | |
# rescue ArgumentError => e | |
# raise | |
# raise e, e.message, e.backtrace | |
# raise e | |
rescue | |
# to preserve backtrace good | |
# raise | |
# raise $! | |
# raise $!, "bad arg yo -- part 2", $!.backtrace | |
# raise $!, $!.message, $!.backtrace | |
#bad | |
# raise $!, "Just smoked the backtrace on existing exception" | |
# raise "Just smoked the backtrace and changed the exception type " | |
# raise RuntimeError.new($!) | |
# raise $!, $!.backtrace | |
end | |
end | |
def doit | |
begin | |
bar | |
rescue | |
puts $!.class | |
puts $! | |
puts $!.backtrace | |
end | |
end | |
doit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment