Exceptions, the Old and Confusing Way

Handling error conditions with most programming languages requires much more work than handling a program that is running properly. It can require a very confusing structure of statements, similar in functionality to Java's ifelse and switch blocks, to deal with errors that might occur.

As an example, consider the following statements, which show the structure of how a file might be loaded from disk. Loading a file is something that can be problematic because of a number of different circumstances—disk errors, file-not-found errors, and the like. If the program must have the data from the file in order to operate properly, it must deal with any of these circumstances before continuing.

Here's the structure ...

Get Sams Teach Yourself Java 2 in 21 Days, Second Edition 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.