What is Exception Propagation in Java? Explain.

Exception propagation 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.

Exception Propagation in Unchecked Exceptions

When an exception happens, Propagation is a process in which the exception is being dropped from to 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 reach the very bottom of the call stack. This is called exception propagation and this happens in case of Unchecked Exceptions.

In the example below, exception occurs in method m() where it is not handled, so it is propagated to previous n() method where it is not handled, again it is propagated to p() method where exception is handled.

Exception can be handled in any method in call stack either in main() method, p() method, n() method or m() method.

Note : By default, Unchecked Exceptions are forwarded in calling chain.

/* Java program to illustrate unchecked exception 
 * propagation without using throws keyword.
 */
class Simple { 
    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[]) 
    { 
        Simple obj = new Simple(); 
        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. Because unlike in the case of unchecked exceptions, the checked exceptions cannot propagate without using throws keyword.

Note : By default, Checked Exceptions are not forwarded in calling chain (propagated).

// Java program to illustrate exception propagation 
// in checked exceptions  and it can be propagated 
// by throws keyword ONLY 
import java.io.IOException; 
class Simple { 
 
    // 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[]) 
    { 
        Simple obj = new Simple(); 
        obj.p(); 
        System.out.println("normal flow..."); 
    } 
} 
 
Output:
 
exception handled 
normal flow...

Hope you have enjoyed reading What is Exception Propagation in Java? Explain. Please do write us if you have any suggestion/comment or come across any error on this page. Thank you for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.