Catch a Ruby exception for logging, but don't throw it?
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)
精彩评论