Rescue exception in Ruby and continue
Usually when we run scheduled jobs we like to bunch a few things together for convenience. Let’s say that you have hourly, daily, weekly, monthly task list that needs to be executed. And that you use cron to execute those tasks. A good approach here would be to have those same cron tasks defined in crontab, one entry for each task. However we rarely do that because we have multiple tasks that can run together at the same time so we define a method that will run them one after another. The thing we usually forget is that one of those methods, usually not the last one, will raise an exception and prevent the other methods from running. Something like:
Now imagine that the update_counters
method fails and no emails get sent and the logs aren’t synced (whatever that means). We don’t want that situation to happen, but we also don’t want to blindly ignore exceptions doing something like this:
Of course we have linters and tools to warn us that this is bad practice, but we are trying to ship the product and it’s late Friday afternoon just before the big trip. A more sane solution would be to run those methods, catch any exceptions that occur and then continue.
You could define every method with a rescue clause:
But if you want to avoid repeating code and just implement the rescue once, you can define a exception catching block method:
This way we can rewrite the example above to be more concise, catch any exceptions that occur and run all good methods.
If you encounter this problem in other places in the code, it’s always easy to extract it into a module and then include that module anywhere else in the code where you need the functionality.
Comments