4. Exceptions

Sometimes an error condition is bad enough that it seems flat out rude to return nil from a method. When things get this mucked up it’s best to unwind the stack and provide details describing the failure.

Purists (of which I’m a card carrying member) believe that exceptions should rarely be used. Mostly to give a meaningful apology to the user. (Followed promptly by crashing the program and discarding all her hard work.)

Ruby programmers tend to be more liberal with exceptions. A number of routine error conditions are encoded into standard exception classes and used to report specific failures. Fortunately, the most common exception classes contain enough information that it’s practical to rescue the exception and recover from the ...

Get Effective Ruby: 48 Specific Ways to Write Better Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.