Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Exceptions occur when a program gets off course, and the normal program flow is interrupted. Ruby is prepared to handle such problems with its own built-in exceptions, but you can handle them in your own way with exception handling. Ruby's exception handling model is similar to the C++ and Java models. Table 10 shows a comparison of the keywords or methods used to perform exception handling in all three languages.
| C++ | Java | Ruby |
|---|---|---|
| try {} | try {} | begin/end |
| catch {} | catch {} | rescue keyword (or catch method) |
| Not applicable | finally | ensure |
| throw | throw | raise (or throw method) |