It is not a matter of IF but WHEN things will go wrong in a computer program. Sometimes there are bugs, errors of one form or another. There are also unforeseen use cases. You can never assume a computer program is perfect. Exception-Handling helps us to catch erroneous events and devise means of correcting them. We discuss this topic here since exception-handling can take more code than should be put into the main line of execution. In such cases, a method in an exception-handling class should be called. Exception handling mechanisms allow a program to continue executing, instead of terminating it abruptly, even if an error occurs in the program.
10.3 Java’s Exception Hierarchy
The Exception Class
The java.lang.Exception class itself is very simple, consisting of
just two constructor methods (Fig. 10.5). The Throwable class, from
which Exception is derived, is the root class of Java’s exception and
error hierarchy. It contains definitions for the getMessage() and
printStackTrace() methods, which are two methods that we will use
frequently in our error-handling routines.
SELF-STUDY EXERCISE
EXERCISE 10.2 Which of the following are examples of unchecked exceptions?
a. IOException
b. IndexOutOfBoundsException
c. NullPointerException
d. ClassNotFoundException
e. NumberFormatException