views:

132

answers:

2

I have something like this:

class Vehicle

  def self.set_color(input)
     if %w{blue red green}.include?(input)
       input
     else
       raise "Bad color"
     end
  end

end

class Car < Vehicle

   def make_car
      begin
        my_color = Vehicle.set_color("orange")
      rescue
        puts "you screwed the pooch"
      end
   end

end

class CarTest < Test::Unit::TestCase
   def test_number_one
     c = Car.new
     c.make_car
   end
end

But for some reason, my test is raising the exception and stopping execution instead of catching it and outputting "you screwed the pooch." Any idea why this is happening and how to fix it?

Thanks!

A: 

I am 99% sure "in" is a protected keyword in ruby. Try using a different variable name.

phantombrain
That it is; for i in some_collection (...)
Ed Swangren
the code above is not the exact code, just something simple enough to demonstrate the idea. i don't use "in" in the actual program so that is not the issue. i edited the post to reflect that
Tony
+10  A: 

rescue without an argument is not a "catch-all" for exceptions.

If you just issue a "rescue" it will only rescue a StandardError exception (which will catch a RuntimeError < StandardError) , but not an Exception.

If you really want to catch everything, you should do a


rescue Exception
klochner