Exceptions

Although you are just learning about them now, you have probably become well-acquainted with exceptions during the last 17 previous hours. These errors turn up when you write a Java program that compiles successfully but encounters a problem when it runs.

For example, a common programming mistake is to refer to an element of an array that doesn't exist, as in the following statements:

String[] greek = { "Alpha", "Beta", "Gamma" };
System.out.println(greek[3]);

In this example, the String array has three elements. Because the first element in an array is numbered 0 rather than 1, the first element is greek[0], the second is greek[1] and the third is greek[2]. The statement attempting to display the contents of greek[3] is erroneous, ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH 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.