Errors and Exceptions

By now you've seen what Ruby usually does when an error occurs: It stops and prints a message on the screen. You see a line number, and a description of the problem, and generally you can use that information to go back and fix the script.

But not all unexpected failures are the programmer's fault. There are surprises that don't qualify as emergencies and so should not be allowed to bring a program to a halt. Plenty of strange things can happen when the outside world gets involved. You try to read a file; it doesn't exist. You ask a user for input; you get back something unsuitable. You try to write to disk; the disk is full, or you don't have the right permission, or the file already exists and is marked read-only. So we ...

Get Sams Teach Yourself Ruby in 21 Days 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.