我有这样的事情:
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
但出于某种原因,我的测试是提高异常并停止执行而不是捕获它并输出“你搞砸了小狗”。知道为什么会这样,以及如何解决它?
谢谢!
答案 0 :(得分:11)
如果您只是发出“救援”,它只会挽救一个StandardError异常(它将捕获RuntimeError&lt; StandardError),但不会引发Exception。
如果你真的想要抓住一切,你应该做一个
rescue Exception
答案 1 :(得分:0)
我99%肯定“in”是ruby中受保护的关键字。尝试使用其他变量名称。