Java Reference
In-Depth Information
Throwable
Exception
Error
RuntimeException
IOException
AWTError
ThreadDeath
VirtualMachineError
ClassCastException
NullPointerException
ArithmeticException
IndexOutOfBoundsException
NoSuchElementException
InputMismatchException
ArrayIndexOutOfBoundsException
Fig. 11.4 | Portion of class Throwable 's inheritance hierarchy.
Checked vs. Unchecked Exceptions
Java distinguishes between checked exceptions and unchecked exceptions . This distinction
is important, because the Java compiler enforces special requirements for checked exceptions
(discussed momentarily). An exception's type determines whether it's checked or unchecked.
RuntimeException s Are Unchecked Exceptions
All exception types that are direct or indirect subclasses of RuntimeException (package
java.lang ) are unchecked exceptions. These are typically caused by defects in your pro-
gram's code. Examples of unchecked exceptions include:
ArrayIndexOutOfBoundsException s (discussed in Chapter 7)—You can avoid
these by ensuring that your array indices are always greater than or equal to 0 and
less than the array's length .
ArithmeticException s (shown in Fig. 11.3)—You can avoid the Arithmetic-
Exception that occurs when you divide by zero by checking the denominator to
determine whether it's 0 before performing the calculation.
Classes that inherit directly or indirectly from class Error (Fig. 11.4) are unchecked , be-
cause Error s are such serious problems that your program should not even attempt to deal
with them.
Checked Exceptions
All classes that inherit from class Exception but not directly or indirectly from class Run-
timeException are considered to be checked exceptions. Such exceptions are typically
caused by conditions that are not under the control of the program—for example, in file
processing, the program can't open a file if it does not exist.
 
 
Search WWH ::




Custom Search