Today i was reading the chapter 10 of my Programming Ruby book, about Ruby Exception Handling, Catch and Throw. Found some really interesting stuff, as usual, and i´m gonna write it all down here.

Exception Objects

Ruby lets you insert exception/error information into an object of class Exception (or its children). This object is passed to the preceding code, searching for a declaration that some code is capable of handling that exception. You can create your own Exception classes. To do this, all you need is to make your class children of StandardError or one of its children. It´s as simple as it gets:

class SomeVeryNastyError < StandardError

Error Handling

Things useful to know when dealing with error handling:

  • To cast an exception, just use the raise or Kernel.raise method. Arguments can be: raise [Exception class, [Message, [Callback array]]]
  • The code that is gonna have exception handling should be wrapped inside a begin/rescue/end block if it´s multilined or you can use rescue as a statement modifier for single-lines. The arguments to rescue clause are the exception classes it can handle (comma-separated) and an optional local variable to store a reference to the Exception object.
    # some code...
    rescue MyError, BadError, StandardError => err_reference
    #rescue stuff...
  • Along with rescue, there are the else clause and the ensure clause:
    Code inside the else clause will execute if no exceptions happen;
    Code inside the ensure clause is always executed when the block terminates.

      # some code is executing and..
      raise SomeVeryNastyError, "Very nasty error occurred!"
    rescue SomeVeryNastyError => err_reference
      # here comes the rescue!
      puts "An error ocurred: #{err_reference.inspect}"
      # rescue code ...
      puts "No error occurred!"
      # having an error or not ...
      puts "execution ended!"
  • There can be multiple rescue clauses within a block.
    #some code
    rescue ForcedError
    puts "Forced Error occurred!"
    rescue StandardError
    puts "Standard Error occurred!"
  • A reference to the associated raised Exception object is placed into the global variable $!
  • Within a rescue clause, you can call the retry method, so that the code block begins its execution again.
  • You can add your own attributes and methods to your Exception classes.
    class MyError < StandardError
    attr_reader :ok_to_retry
    def initialize(retry)
    @ok_to_retry = retry
    #some code ...
    raise MyError(true)
    rescue MyError
    if $1.ok_to_retry
        puts "Retrying..."
        puts "Not retrying!"

Catch and Throw

If you ever need to get a signal to terminate execution of some code, you will be in good hands when using catch and throw.

Firstly, you specify what to “catch”: it can be a Symbol or a String. A code block comes after and will be executed until it runs to the end or until the specified catch argument is thrown with the throw method. The throw method supports two arguments: a Symbol/String and a return value.

a = catch(:fire) do
  puts "Catching fire now!"
puts a
# => fireee!

And that´s it for today! I´m heading towards chapter 11 of Programming Ruby tonight!
Feel free to post comments and examples in order to help me and the other fellas!

Other references: Programming RubySkorks post on exceptionsGoogle