我有一个自定义异常,我希望在执行该方法导致错误时多次引发和救援。我知道它最终会导致无异常结果。
使用 begin/rescue/end 似乎是在抛出异常并调用救援块时,如果再次抛出异常,程序将离开 begin/rescue/end 块并且错误结束程序。如何保持程序运行直到达到正确的结果?另外,我对正在发生的事情的看法是否不正确?
这基本上就是我想要发生的事情(但显然尽可能使用 DRY 的代码......这段代码只是为了说明而不是我要实现的)。
ships.each do |ship|
begin
orientation = rand(2) == 1 ? :vertical : :horizontal
cell_coords = [rand(10), rand(10)]
place_ship(ship, orientation, cell_coords)
rescue OverlapError #if overlap error happens twice in a row, it leaves?
orientation = rand(2) == 1 ? :vertical : :horizontal
cell_coords = [rand(10), rand(10)]
place_ship(ship, orientation, cell_coords)
rescue OverlapError
orientation = rand(2) == 1 ? :vertical : :horizontal
cell_coords = [rand(10), rand(10)]
place_ship(ship, orientation, cell_coords)
rescue OverlapError
orientation = rand(2) == 1 ? :vertical : :horizontal
cell_coords = [rand(10), rand(10)]
place_ship(ship, orientation, cell_coords)
#keep rescuing until the result is exception free
end
end