我有一个rake任务,它遍历CSV文件中的行,在该循环中,有一个begin/rake块来捕获任何可能引发的异常。但是当我运行它的时候,它不停地说‘耙流产了!’它没有进入救援队
CSV.foreach(path, :headers => true) do |row|
id = row.to_hash['id'].to_i
if id.present?
begin
# call to mymethod
rescue => ex
puts "#{ex} error executing task"
end
end
end
...
def mymethod(...)
...
begin
response = RestClient.post(...)
rescue => ex
raise Exception.new('...')
end
end预期:它应该完成CSV的所有行的循环。
实际结果:在达到“提高”例外后停止,并指出:
拉克流产了! 异常:这里的错误消息 ..。 原因如下: RestClient::InternalServerError: 500内部服务器错误
发布于 2019-01-23 19:02:21
我解决了这个问题,只需注释掉引起异常的行,因为它似乎是目前最快的解决方法。
# raise Exception.new('...')如果有更好的方法,我仍然愿意听取其他建议。
发布于 2019-01-23 14:16:46
您可以使用next跳过循环的错误步骤:
CSV.foreach(path, :headers => true) do |row|
id = row.to_hash['id'].to_i
if id.present?
begin
method_which_doing_the_staff
rescue SomethingException
next
end
end
end并在方法中引发异常:
def method_which_doing_the_staff
stuff
...
raise SomethingException.new('hasd')
endhttps://stackoverflow.com/questions/54328902
复制相似问题