Subscribe to RSS Feed

Elijah Miller

19 Feb 2009

Ruby's Rescue Statement Can Has Value?

I just discovered something interesting about Ruby’s exception rescuing. Check out these two example methods that differ only by exception assignment.

def return_nil_on_error
  begin
    yield
  rescue
  end
end

def return_exception_on_error
  begin
    yield
  rescue => err
  end
end

puts return_nil_on_error { raise "error!" }.inspect
puts return_exception_on_error { raise "error!" }.inspect

Running the code above and this is what you will see.

nil
#<RuntimeError: error!>

It’s interesting to see that Ruby treats the a rescue block as a value expression if it assigns the exception to a variable. Is there a valid use for this oddity?