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!

Tony
  • 18,776
  • 31
  • 129
  • 193
  • Does this happen if you do `c.make_car` outside the TestCase? It outputs "you screwed the pooch" as it should for me.. – dbr Aug 11 '09 at 01:34

2 Answers2

11

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
  • 8,077
  • 1
  • 33
  • 45
  • 1
    Which you should very rarely do: http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby – cbmanica Jan 09 '14 at 17:59
0

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

Cullen Walsh
  • 4,318
  • 1
  • 17
  • 12
  • That it is; for i in some_collection (...) – Ed S. Aug 11 '09 at 00:28
  • 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 Aug 11 '09 at 01:15