Errors

Errors

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

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

Loading...

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

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:

Loading...
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn Ruby on Codecademy