Open In App

Exception Propagation in Java

Last Updated : 06 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, an exception is first thrown from the top of the stack, and if it is not caught, it drops down the call stack to the previous method. After a method throws an exception, the runtime system attempts to find something to handle it. The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred. The list of methods is known as the call stack, and the method of searching is Exception Propagation.

The image below demonstrates the flow of method calls in a program

Exception Propagation in Unchecked Exceptions

When an exception happens, propagation is a process in which the exception is dropped from the top to the bottom of the stack. If not caught once, the exception again drops down to the previous method, and so on until it gets caught or until it reaches the very bottom of the call stack. This is called exception propagation, and this happens in case of Unchecked Exceptions.

In the example below, an exception occurs in the m() method, where it is not handled, so it is propagated to the previous n() method, where it is not handled, again it is propagated to p() method, where the exception is handled. Exceptions can be handled in any method in the call stack, either in the main() method, p() method, n() method, or m() method.

Note: By default, unchecked exceptions are forwarded in the calling chain (propagated).

Example:

JAVA
// Java program to illustrate
// unchecked exception propagation
// without using throws keyword
class Geeks {
    void m()
    {
        int data = 50 / 0; // unchecked exception occurred
        // exception propagated to n()
    }

    void n()
    {
        m();
        // exception propagated to p()
    }

    void p()
    {
        try {
            n(); // exception handled
        }
        catch (Exception e) {
            System.out.println("Exception handled");
        }
    }

    public static void main(String args[])
    {
        Geeks obj = new Geeks();
        obj.p();
        System.out.println("Normal flow...");
    }
}

Output
Exception handled
Normal flow...


Exception Propagation in Checked Exceptions

Unlike Unchecked Exceptions, the propagation of exception does not happen in case of Checked Exception and its mandatory to use throw keyword here. Only unchecked exceptions are propagated. Checked exceptions throw compilation error.

In example below, If we omit the throws keyword from the m() and n() functions, the compiler will generate compile time error this is because checked exceptions cannot propagate without the explicit use of the throws keyword.

Note: By default, checked exceptions are not forwarded in calling chain (propagated). They must be declared in the method signature using the throws keyword.

Example:

JAVA
// Java program to illustrate exception propagation
// in checked exceptions  and it can be propagated
// by throws keyword ONLY
import java.io.IOException;

class Geeks {

    // exception propagated to n()
    void m() throws IOException
    {
        // checked exception occurred
        throw new IOException("device error");
    }

    // exception propagated to p()
    void n() throws IOException
    {
        m();
    }
    void p()
    {
        try {

            // exception handled
            n();
        }
        catch (Exception e) {
            System.out.println("Exception handled");
        }
    }

    public static void main(String args[])
    {
        Geeks obj = new Geeks();
        obj.p();
        System.out.println("normal flow...");
    }
}

Output
Exception handled
normal flow...


Unchecked Exceptions vs Checked Exceptions

The table below demonstrates the difference between unchecked and checked exception.

Property

Unchecked Exceptions

Checked Exceptions

Propagation

Propagate automatically through the call stack.

Do not propagate automatically.

Throws Keyword

Do not require the throws keyword

It requires a throw keyword

Compile-Time Check

Not checked at compile-time

Checked at compile-time,

Example

ArithmeticException, NullPointerException, IndexOutOfBoundsException.

IOException, SQLException


Next Article

Similar Reads