Exception Handling in Java – Exception Hierarchy in Java

1. Objective

In this Java tutorial, we are going to learn about what is Java exception with examples and some key features that are to be kept in mind while Java Exception Handling. Moreover, we will study the exception hierarchy in Java and how JVM handles an exception in Java. At last, we discuss exception handling in Java by a programmer and by Java Virtual Machine (JVM).

So, let’s discuss exception handling in Java.

Exception Handling in Java - Exception Hierarchy in Java

Exception Handling in Java – Exception Hierarchy in Java

2. What is Java Exception?

Exception in Java is an unwanted or unexpected event that occurs during the exception of any Java program when the compiler compiles the program, it changes the normal course of the program, and i.e. how the programmer wanted it to run.

An error and exception are totally different things, let us have a look at both of them-

  • Error – An error is a problem which is serious and any program shouldn’t face it.
  • Exception – An exception is a problem that is not that serious and a program may or may not want it.

Do you know What is Java Exception?

3. Exception Hierarchy in Java

All errors and exceptions in java belong to the subclass ‘Throwable’, which itself is a base class of hierarchy. One branch head by error and other by exception, Java exception is the condition that the program should catch while errors should avoid. NullPointerException is an example of exception while StackOverflowError is an example of error.

Exception Hierarchy in Java - Java Exception

Java Exception – Exception Hierarchy in Java

Java Quiz

4. Exception Handling in Java By JVM

When an exception occurs in a program, Java method creates an object named ‘Exception object’, this Java object is transferred to runtime system (Java Virtual Machine). The Exception object contains the details of the details about the exception, i.e. the name, description and also the current status of the program after the exception has occurred.  The process of creation and handling the exception rum time system call as Throwing an Exception. There is a list of methods that call during an exception, the ordered list is call as a Call Stack.

Let’s explore Difference Between Checked and Unchecked Exceptions in Java

The steps involved in Exception Handling in Java are –

  • The runtime system of Java searches for the required Call Stack that can handle the exception that has incurred in the system. The called method is called the ‘Exception handler’.
  • The runtime system searches for the exception in the method and proceeds in the reverse order of calling the exception.
  • An appropriate handler searches and assigned to the exception.
  • If no appropriate exception handler found then the default handler handles it.

Exception in thread “xxx” Name of Exception: Description
… …… ..  // Call Stack

Java Exception Handling - JVM

Java Exception Handling By JVM

Example to demonstrate how an exception is thrown

class ThrowsExecp
      public static void main(String args[])
            String str = null;

Example program to demonstrate exception is thrown 

class ExceptionThrown
        static int divideByZero(int a, int b)
             int i = a/b;
             return i;
       static int computeDivision(int a, int b)
                    int res =0;
                              res = divideByZero(a,b);
        // doesn't matches with ArithmeticException
           catch(NumberFormatException ex)
                                   System.out.println("NumberFormatException is occured");
        return res;
 public static void main(String args[]){
         int a = 1;
         int b = 0;
              int i = computeDivision(a,b);
        catch(ArithmeticException ex)

Read about Java Regular Expression in Detail

5. Exception Handling in Java by Programmer

An exception handling using the five keywords, namely, try, catch, throw, throws, and finally. Exception Handling in Java, if an exception is to manually insert then the keyword throw use. It should be specified in the throw clause.

// java program to demonstrate need of try-catch clause
class GFG
          public static void main (String[] args)
        // array of size 7
                    int[] arr = new int[7];
        // this causes an exception
                     int i = arr[4];
        // this will never execute
Java Exception Handling - By Programmer

Java Exception Handling by Programmer

This was all about Exception Handling in Java tutorial. Hope you like our explanation.

6. Conclusion

Hence, we studied how Java exception occurs, Java exception hierarchy and Exception Handling in Java by JVM and Programmer with output by the system. Still, have a confusion? Feel free to share with us!

See also – Keyword in Java

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.