Exception Handling - Exception Hierarchy

Exception Handling - Exception Hierarchy
The Root of all the Exception and Errors in java is Throwable class.

Please note: "Throwable is a class not an Interface."

Throwable has two sub Classes

1. Exception
2. Error

Exception : Mostly Exceptions are caused by the our program, For example

package com.learning.exceptionhandling;

public class SampleClass {

 public static void main(String[] args) {

  String[] stringArray = new String[9];
  for (int i = 0; i < 10; i++) {
   stringArray[i] = "It has the value : " + i;
                        System.out.println("It has the value : " + i);
  }

 }
}

Output:

It has the value : 0
It has the value : 1
It has the value : 2
It has the value : 3
It has the value : 4
It has the value : 5
It has the value : 6
It has the value : 7
It has the value : 8
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
 at com.learning.exceptionhandling.SampleClass.main(SampleClass.java:10)


In the below class we have taken an array of size 9, now we try to access the 10th block in that array our application thrown an exception.

Exceptions can be recovered  by handling the code so that rest of the flow of the application can continue.

Error : These type of issues are not caused by the application which we built, it occurs due to in sufficient system resources available to run the application that we built. Errors are not recoverable we have to just to terminate the application.

Best example for an Error is OutOfMemory Error, this caused when the application is not having enough heap.

 Exceptions has many Sub Classes, but below are the major sub classes of Exception.

1. Runtime Exceptions 
            a. NullPointerException
            b. ArithmeticException
            c. ClassCastException.
            d. IndexOutOfBoundException.
            e. IllegalArgumentException
            
2. Io Exceptions
       a. FileNotFoundException
       b. EOFException
       c. InterruptedIOException.
3. Remote Exceptions
4. Interrupted Exceptions
5. Servlet Exception.


Error has Sub Classes as below

1. Vm Error: In Vm Error we see mostly two common errors
                a. StackOverFlow Error
                b. OutOfMemory Error
2. Assertion Error
3. ExceptionInInitializer Error.