Ruby Errors

christian.dinh's avatar
Published Jul 29, 2021Updated May 15, 2024
Contribute to Docs

Ruby uses exceptions to report errors and other information when something in the program has gone wrong.

If an error has occurred, the Ruby program will end. For example:

puts 2 / 0

This will result in:

Traceback (most recent call last):
        1: from errors_in_ruby.rb:1:in `<main>'
errors_in_ruby.rb:1:in `/': divided by 0 (ZeroDivisionError)

Some common errors are:

  • RuntimeError
  • StandardError
  • NoMethodError
  • IOError
  • TypeError
  • ArgumentError
  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn to program in Ruby, a flexible and beginner-friendly language used to create sites like Codecademy.
    • Beginner Friendly.
      9 hours

Rescuing Errors

Use the rescue keyword in a begin...end block to catch errors and report messages. The error can be as general as StandardError or as specific as ZeroDivisionError:

begin
puts 2 / 0
rescue ZeroDivisionError
puts "Can't divide by 0"
end

All contributors

Contribute to Docs

Learn Ruby on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn to program in Ruby, a flexible and beginner-friendly language used to create sites like Codecademy.
    • Beginner Friendly.
      9 hours