开发者

Catch a Ruby exception for logging, but don't throw it?

开发者 https://www.devze.com 2022-12-20 04:32 出处:网络
I seem开发者_JAVA技巧 to be having a brain freeze.I want to catch a possible Ruby exception during a loop through several objects in order to count it as a failure for displaying later, but I do not w

I seem开发者_JAVA技巧 to be having a brain freeze. I want to catch a possible Ruby exception during a loop through several objects in order to count it as a failure for displaying later, but I do not want execution halted; I want it to skip the bad record and continue. How do I do this again? I don't think I can use retry because that would try the same record again, right?


some_ary.each do |item|
  begin
    do_something_with item
  rescue Exception => e
    Logger.error "OH NO: #{e}"
  end
end

Execution should continue with errors caught and logged.


Don't "do" anything. If you've caught an exception and handled it (by adding the bad object to a list or whatever), just continue on your way. Pseudo-code:

for each object foo
    try
        do something risky with foo
    catch
        badobjects.add(foo)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号