-
Book Overview & Buying
-
Table Of Contents
Polished Ruby Programming - Second Edition
By :
Raising exceptions is the most common way to handle errors in Ruby. Almost all core methods in Ruby can raise an exception when called with an unexpected number of arguments:
"S".length(1)
# ArgumentError (wrong number of arguments)
The exceptions would be methods that do not require any arguments and accept an arbitrary number of arguments, such as Hash#values_at and Enumerable#chain.
In many cases, you can also get a core method to trigger an exception when passing the wrong type of argument:
'S'.count(1)
# TypeError (no implicit conversion of Integer into String)
In almost all cases, unexpected or uncommon errors should be raised as an exception, and not handled via a return value. Otherwise, you end up with a case where the error is silently ignored. In the previous section, you saw an example where the update method using a return value to signal an error can result in data loss. However, there are other cases where the results...
Change the font size
Change margin width
Change background colour