In Ruby, is it possible to rescue all exceptions except for a specified one?
3 Answers
Not as such no. But you can rescue all exceptions and reraise the exception if it is a MyException.
2 Comments
Will Sheppard
You're not supposed to rescue all exceptions: stackoverflow.com/questions/10048173/…
Orlando
@WillSheppard I would argue that this depends entirely on what your code is doing.
Without knowing more about your problem, I'd suggest Ken Bloom's answer.
However, I'd like to know more about why you're doing it.
Are you worried about a really severe exception, and not wanting to rescue that one, but allow less severe exceptions to be rescued?
In that case, I'd make my custom exception inherit from Exception rather than StandardError and then have
begin do_risky_stuff rescue # Not rescue Exception handle_less_serious_stuff end