Error Handling in Ruby 1.9.2
I have a Ruby method that iterates through a loop (loop_a
) and calls another function (do_something
) each time through the loop. I want Rub开发者_如何学Cy to go to the next cycle of loop_a
even if it has the function do_something
throws an error. I am using the exception handling code below but the application continues to stop on the error. Can someone tell me what I am doing wrong?
loop_a.each do |str1|
do_something(str1)
rescue Exception => e
Logger.error 'An error occurred: #{e}"
end
You need to have a begin
, rescue
, & end
. You also opened with a single-quote and closed with a double-quote.
loop_a.each do |str1|
begin
do_something(str1)
rescue Exception => e
Logger.error "An error occurred: #{e}"
end
end
You may wish to read more on exception handling in Ruby.
精彩评论