I am learning more and more about ruby and have written a script that times out on occassion (making several API calls).
I know I need to rescue
the exception with something like this:
rescue Timeout::Error => e
# log #{e} for later, maybe
end
My question is where I should place that rescue
block. Since I am making several API calls within multiple loops, would I need to put that block within each API loop? I would prefer to just write the rescue
block once, at the end of the script for example, and have it work inside each loop in the script. I am using ruby 1.9.3.
It mostly depends of what do you try to do with the errors.
For example, if you want the errors not to abort the loop run, catch the exception inside the loop, you can log the error and continue with the next iteration.
If the error is kind of "fatal" and you cannot (or don't want to) continue with the loop, you catch it outside.