Skip to content

Instantly share code, notes, and snippets.

@Iristyle
Created August 8, 2017 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Iristyle/a1b291f11cc853ab44aeea531462158b to your computer and use it in GitHub Desktop.
Save Iristyle/a1b291f11cc853ab44aeea531462158b to your computer and use it in GitHub Desktop.
Ruby error handling approaches
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