Ruby 3 · Errors and files

Rescue exceptions

Handle errors with begin/rescue/ensure. Rescue specific classes, not Exception.

begin
  Integer("abc")
rescue ArgumentError => e
  puts "bad number: #{e.message}"
ensure
  puts "always runs"
end

Official Ruby 3 documentation

More errors and files commands

All errors and files commands · Ruby 3 cheatsheet

Was this page helpful?