219. Exception Hierarchy

Java throws a different types of Exceptions (i.e. objects)  based on the exceptional conditions that are occurred in the written Program code. All the different exceptions (i.e. objects) have their own Class types (i.e.Exception Types) . Please find the Exception Hierarchy below -

1. Throwable Class is the parent class of all the Exception Classes (i.e. All Exception Types)

2. Error and Exception Classes are the sub-classes of Throwable Class. Error and Exception Classes will have their own sub-classes (As there are many sub-classes under these two Classes, I have not listed them in the above hierarchy diagram)

3. All the sub-classes of Exception Class can be categorized into two groups 'Runtime Exceptions' and 'Other Exceptions' as shown in the above diagram.

Hoping that you have understood the above basic Hierarchy of Exceptions, I would like to present the advanced version of Exception Hierarchy below by expanding the Classes in the above basic hierarchy -

1. Throwable Class is the parent class of all the Exception Classes below it in the above diagram, hence all the Exception Classes inherits the methods of the Throwable Class.

2. Error and Exception Classes are the sub-classes of Throwable Class

3. Error Class and its sub-classes handles the unrecoverable exceptional conditions like Out of Memory, Stack Over Flow exceptions in our program code internally (i.e. within JVM). These exceptions can't be handled by the programmers using the statements like try{ } catch{ } etc. When the unrecoverable exceptions like Stack Overflow or Out of Memory exceptions are occurred, Error Class will handle the exception within JVM and the JVM will terminate the program without executing the remaining steps in the program which are after the statement that caused this exception. JVM throws the unrecoverable exceptional error conditions to Error Class or its

4. Exception and its sub-classes handles the recoverable exceptional conditions in our program code. And the exceptions handled by these Classes are categorized into two categories A) Run-Time Exceptions B) Checked Exceptions based on whether they are checked by the compiler.

A) Run Time Exceptions are not checked by the compiler during the compilation process, hence we can also call them as Unchecked Exceptions. Run time exceptions occur during the program execution process.  Example - The program statement int a = 1/0; wont give any error while compiling the program, but will throw error while the program is executing. As this exception is not checked during compile time, we call them as Unchecked Exceptions.

B) Checked Exceptions are the exceptions that are checked by compiler. Compiler gives compiler errors when any such exception condition occur.

5. The Run Time Exceptions category has the sub-classes of Exception Class. i.e. ArithmenticException, ClassCastException, IllegalArgumentException, NullPointException etc. are the sub-classes of Exception Class which belong to its Run Time Exceptions category. All the exceptions which are handled by the sub-classes in the Run Time Exceptions category, are not checked by the compiler. Hence they are called as unchecked exceptions.

Please comment below to feedback or ask questions.

Catching Exceptions using Throwable Class will be explained in the next post.

No comments: