Exceptions in Ruby - throwing, catching and using
Archive - Originally posted on "The Horse's Mouth" - 2016-05-17 13:07:16 - Graham EllisIt's far better to use exceptions to trap run time irregularities than to try to forecast all possible errors, as exceptions form a sort of safety net. From today's Ruby Course, [here] is an example where the user is prompted to enter an integer, and I deliberately use the Integer function rather than to_i to extract that number so that I can catch incorrect entries through an exception (Integer throws exceptions, but to_i just returns a 0).
A further example - [here] - shows how exceptions can be caught in methods or thorn up to parents, and how methods can even raise their own exceptions. If you're writing methods, it's sensible to throw exceptions up as it forces the person using you code to take into account situations where no value is to be returned.