Saving the Error

suggest change

Assign a rescued exception to a variable when its details are needed in the handler:

def divide(x, y)
  x / y
rescue StandardError => error
  warn "There was a #{error.class} (#{error.message})"
  warn error.full_message(highlight: false, order: :top)
  nil
end

divide(10, 0)   # reports ZeroDivisionError: divided by 0
divide(10, 'a') # reports TypeError: String can't be coerced into Integer

Ruby 4 uses the unified Integer class; the old Fixnum name no longer exists.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents