Exceptions

Raise

The keyword raise stops the program with an error object. A plain string becomes a RuntimeError carrying that message. Naming a class with a message pinpoints the failure more precisely.

raise "boom"

ruby raise_demo.rb

raise_demo.rb:1:in '<main>': boom (RuntimeError)

raise ArgumentError, "bad input"

ruby raise_class_demo.rb

raise_class_demo.rb:1:in '<main>': bad input (ArgumentError)

raise ArgumentError, "bad input"
      ^^^^^^^^^^^^^^^^^^^^^^^^^^

Left alone, either line ends the program and prints the message with a backtrace.

Rescue

The keywords begin and rescue wrap risky code: the rescue branch runs when the body raises. Naming an error class catches only that family, while other errors keep traveling upward. Match the class to the failures you truly expect.

begin raise "boom" rescue RuntimeError puts "caught" end

ruby rescue_demo.rb

caught

Ensure always runs

The keyword ensure closes a begin block with cleanup that always runs: after success, after a rescued failure, and even when an unexpected error escapes. Closing files and releasing locks belongs there.

begin raise "boom" rescue RuntimeError puts "caught" ensure puts "cleanup" end

ruby ensure_demo.rb

caught
cleanup

Your own error class

Subclassing StandardError defines an error type of your own, carrying the intent of the domain. Rescuing it by name keeps application failures apart from ordinary bugs. Give the class a clear name so future readers grasp the rescue at a glance.

class AppError < StandardError end begin raise AppError, "nope" rescue AppError puts "app failed" end

ruby custom_error_demo.rb

app failed

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does raise "boom" raise?

    Answer

    A RuntimeError carrying the message "boom"

    A plain string becomes a RuntimeError with that message.

  2. What does this program print? begin raise "boom" rescue RuntimeError puts "caught" end

    Answer

    caught

    The rescue names the class that was raised, so its branch runs.

  3. What does this program print? begin raise "boom" rescue RuntimeError puts "caught" ensure puts "cleanup" end

    Answer

    caught / cleanup

    The rescue prints first, then ensure runs its cleanup.

  4. What does this program print? class AppError < StandardError end begin raise AppError, "nope" rescue AppError puts "app failed" end

    Answer

    app failed

    A subclass of StandardError is rescued by its own name.

  5. Which class should a custom application error inherit from?

    Answer

    StandardError

    Subclassing StandardError gives an ordinary error that a plain rescue catches.

  6. What does this program do? begin raise ArgumentError, "x" rescue RuntimeError puts "r" end

    Answer

    an ArgumentError, because the rescue names RuntimeError

    A rescue catches only the class it names and its subclasses; ArgumentError is not a RuntimeError.

  7. What does this program print? begin puts "done" ensure puts "cleanup" end

    Answer

    done / cleanup

    ensure runs after the body, also when nothing fails.

  8. What does this program print? begin raise "boom" rescue => e puts e.message end

    Answer

    boom

    A bare rescue catches StandardError, and e.message is the text of the raise.

  9. An unexpected error escapes a begin block that has an ensure. What happens?

    Answer

    The ensure block runs, then the error continues

    ensure always runs, which is why files and locks are released there.

  10. What does this program print? class AppError < StandardError end p AppError.ancestors.include?(StandardError)

    Answer

    true

    A subclass of StandardError has it in the ancestors chain, so a plain rescue catches it.

Article author: Arthur Isaev