无法捕获Ruby中的异常

无法捕获Ruby中的异常,ruby,exception-handling,Ruby,Exception Handling,我有这样的想法: 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_col

我有这样的想法:

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
class车辆
def自我设置颜色(输入)
如果%w{蓝-红-绿}。包括?(输入)
输入
其他的
提高“坏颜色”
结束
结束
结束
车辆类别
def制造汽车
开始
我的颜色=车辆。设置颜色(“橙色”)
营救
写着“你搞砸了狗”
结束
结束
结束
类CarTest
但出于某种原因,我的测试是引发异常并停止执行,而不是捕获异常并输出“你搞砸了狗”。你知道为什么会发生这种情况以及如何修复它吗


谢谢

我99%确定“in”在ruby中是一个受保护的关键字。尝试使用不同的变量名。

对于异常,不带参数的rescue不是“包罗万象”

如果您只是发出一个“rescue”,它将只拯救一个StandardError异常(它将捕获一个RuntimeError 如果你真的想抓住一切,你应该做一个


rescue Exception

的确如此;对于我来说,在一些集合(…)中,上面的代码不是精确的代码,只是一些足够简单的东西来演示这个想法。我在实际程序中不使用“in”,所以这不是问题所在。我编辑了这篇文章,以反映如果你在测试用例之外做了
c.make\u car
,这会发生吗?它输出了“你把狗给搞砸了”这对我来说应该是这样的…你很少会这么做: