Exception Hierarchy and Types
Java exception handling is used to handle error conditions in a program systematically by taking the necessary actions. Therefore stated simply, the exception-handling capability of Java makes it possible for us to: All exception types are subclasses of the Java built-in class java.lang.Throwable (which is a subclass of java.lang.Object). Thus, Throwable is at the top of the exception class hierarchy. Immediately below Throwable are two subclasses that partition exceptions into two distinct branches: java.lang.Exception and java.lang.Error. The objects that inherit from the Throwable class include direct descendants (objects that inherit directly from the Throwable class) and indirect descendants (objects that inherit from children or grandchildren of the Throwable class). Figure 1 below illustrates the class hierarchy of the Throwable class and its most significant subclasses.
Figure 1: Class Hierarchy of the java.lang.Throwable class
java.lang.Exception
One branch is headed by java.lang.Exception. This class is used for exceptional conditions that user programs should catch. This is also the class that we will subclass to create our own custom exception types. There is an important subclass of Exception, called java.lang.RuntimeException. Exceptions of this type are automatically defined for the programs that we write and include things such as division by zero, invalid array indexing etc. E.g. ArithmeticException, ArrayIndexOutOfBoundsException, IOException etc.
java.lang.Error
The other branch is led by java.lang.Error, which defines exceptions those are not expected to be caught under normal circumstances by our program. Exceptions of type Error are used by the Java runtime system to indicate errors having to do with the runtime environment, itself. Stack overflow is an example of such an error. Exceptions of type Error are typically created in response to catastrophic failures that cannot usually be handled by user program. E.g. VirtualMachineError, OutOfMemoryError, StackOverflowError etc.
Therefore, there are three types of Java exceptions: Exception, Error, and RuntimeException (though it is a subclass of Exception).
Checked and Unchecked exceptions
When an exception occurs, it must be dealt with in either a “try/catch” block or by declaring a “throws” in a method. This concept is called to catch or to declare an exception object. Now during compile time whether to catch/declare an exception or not, based on this, Java also defines two kinds of exceptions called checked and unchecked exceptions:
∅ Checked exceptions: Compiler will check that we have done one of the two things (catch, or declare). So these are called checked exceptions. Exceptions that inherit from the java.lang.Exception class (except RuntimeException and its subclasses) are checked exceptions. Checked Exceptions force programmers to deal with the exception that may be thrown by the API, either in a catch clause or by forwarding it outward with the throws clause. E.g. Exception, IOException, FileNotFoundException, InterruptedException, ClassNotFoundException etc.
∅ Unchecked exceptions: The class java.lang.RuntimeException and its subclasses are not checked by compiler (even though we can choose to catch, or declare, it is not required). So, these two are called unchecked exceptions. Unlike checked exceptions the subclasses of RuntimeException do not need to be caught at compile time. E.g. ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException, NullPointerException, NumberFormatException etc.
Constructors of the Throwable class are shown below:
Important methods of the Throwable class are shown below: