Exception Handling In Java

By | | Updated : 2021-12-01 | Viewed : 59 times

Constructor In Java

Exceptions Handling plays a key role in Java. With Exception Handling, the developer can handle the runtime errors otherwise it may end with unexpected results. The current tutorial illustrates exceptions in Java, types of exceptions, and exception handling in java with nice examples to get a clear idea.

What Is Exception In Java

As per the document, An event that occurs in programming execution and causes stop normal execution flow is nothing but the exception.

In other words, in runtime, the normal execution flow is stopped and ends with unexpected results due to some reason is called an exception.

What Is Exception Handling In Java

We just understand when exceptions happen in the outline. We will learn in detail what will happen when exceptions occur. Now consider the below example where an exception occurred.

What Is Exception Handling In Java
package com.docsconsole.tutorials.overloading;


public class Exception {

    public void testExecutionFlow(){
        //try {
            System.out.println(" Statement - 1");
            System.out.println(" Statement - 2");
            System.out.println(" Statement - 3");
            int n = 10/0;
            System.out.println(" Statement - 4 with variable "+ n);
            System.out.println(" Statement - 5");
            System.out.println(" Statement - 6");
        /*}catch (Exception e){
            System.out.println(" Due to exception normal execution flow is disturbed.");
        }*/


    }

}

If you call the testExecutionFlow() with Exception instance then the normal execution will be disturbed. Notice you can find the stackTrace given by JVM. When exception handling is not written then JVM will close execution flow abnormally.

Exception Hierarchy in Java

Now will try to look at below-given diagram to find the hierarchy of exceptions in java.

Exception Hierarchy in Java

Notice the above-given diagram for the hierarchy of exceptions. So it is categorized into two parts those are error and exception. Again the error and exception are classified with different groups. We would look into this entire exception hierarchy in a separate guide.

Leave A Reply