begin
do_something
rescue
Logger.write ...
...error handling...
end
The problem is that it is possible that the code inside the rescue raises an exception. For this use case, I want to suppress it.
So, how to wrap the rescue in a rescue to suppress the Exception?
You said it yourself
Exception Inception
There's no special way other than wrapping blocks of code in a
begin rescue end
block:...unless you're using ActiveSupport: http://api.rubyonrails.org/classes/Kernel.html#method-i-suppress
..but it isn't a plain Ruby thing and I noticed you didn't add a rails tag to your question. Though, you can implement
suppress
yourself. Or, just, here take this:Use that in your rescue:
In plain old Ruby it's got to be rescues all the way down (and indeed,
suppress
is just a nested rescue) but let me just state that it's a bad idea to rescue all exceptions, in essence, by not being explicit about what exception you're rescuing, in other words, by not passing an exception class argument torescue
you're implicitly rescuingStandardError
which is the superclass to most exceptions (more info here). This can lead to hard to find bugs...is the same as:
It's much better to know what exception you're rescuing and be explicit about it:
With that figured out you can use a cascade strategy to rescue your exceptions:
With the above only the rescue clause that matches the exception being raised will run however, code in a rescue block won't be rescued by subsequent rescues, only the code in the
begin
block is rescuable.If you want to have a block of code that always runs even in the event of an exception, use the
ensure
keyword: