Java Exception propagation

The process of sending exceptions from called method to the calling method is called exception propagation. If an exception is propagated and if that exception is not caught in that calling method, not only is called method execution but also calling method execution is terminated.

In other words, uncaught exceptions are propagated in the call stack until the stack becomes empty, this propagation is called Exception Propagation.

After a method throws an exception, the runtime system searches the call stack for a method that contains a block of code(exception handler) that can handle the exception. The search begins with the method in which the error occurred and proceeds through the call stack in the reverse order in which the methods were called. When an appropriate handler is found, the runtime system passes the exception to the handler.



Let's say, we have a chain of methods where method3() calls method2() and method2() calls method1(). So when

1) An exception occurs in method3() and in method3() we don’t have any exception handler.

2) Uncaught exception will be propagated downward in stack i.e it will check the appropriate exception handler in the method2().

3) Again in method2 if we don’t have any exception handler then again exception is propagated downward to method1() where it finds an exception handler.

Example: 1: When exception occurred and handled.

Java

public class ExceptionPropagation {

    void method3() {
        int result = 100 / 0; // Exception Generated
    }

    void method2() {
        method3();
    }

    void method1() {
        try {
            method2();
        } catch (Exception e) {
            System.out.println("Exception is handled here");
        }
    }

    public static void main(String args[]) {
        ExceptionPropagation obj = new ExceptionPropagation();
        obj.method1();
        System.out.println("Continue with Normal Flow...");
    }
}

Output

Exception is handled here Continue with Normal Flow...


Approach 2: When an exception occurred and does not handle anywhere.

Java

package com.prac;

public class ExceptionPropagation {

    void method3() {
        int result = 100 / 0; // Exception Generated
    }

    void method2() {
        method3();
    }

    void method1() {

        method2();

    }

    public static void main(String args[]) {
        ExceptionPropagation obj = new ExceptionPropagation();
        obj.method1();
        System.out.println("Continue with Normal Flow...");
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero at com.prac.ExceptionPropagation.method3(ExceptionPropagation.java:6) at com.prac.ExceptionPropagation.method2(ExceptionPropagation.java:10) at com.prac.ExceptionPropagation.method1(ExceptionPropagation.java:15) at com.prac.ExceptionPropagation.main(ExceptionPropagation.java:21)


Exception Handling Using Throws keyword

Java try/ catch block exception handling

Java catches Multiple Exceptions

Exception Handling Using Throw keyword

Java nested try block exception handling

Java custom exception handling

Throw vs throws

Java finally blocks exception handling

Java Exception propagation

Final vs Finally vs Finalize

Exception Handling Interview Questions


No comments:

Post a Comment