Exception Propagation in Java

Exception Propagation in Java with Examples

In this article, I am going to discuss Exception Propagation in Java with Examples. Please read our previous article where we discussed How to Create Custom Exceptions in Java with Examples. At the end of this article, you will understand the following two pointers in detail.

  1. Exception propagation or chained exception in Java.
  2. How to Re-throwing an Exception in Java?
What is Exception propagation or chained exception in Java?

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. Let us see an example:

What is Exception propagation or chained exception in Java

The Exception occurs in method4() and If the exception is not handled in any method then the exception is thrown and the application is stopped abnormally.

Let us see a program to understand this concept
class ExceptionPropagation
{
    public void method4 ()
    {
        int a = 10, b = 0;
        int c = a / b;
    }
    public void method3 ()
    {
        this.method4 ();
        System.out.println ("after method 4 compltion");
    }
    public void method2 ()
    {
        this.method3 ();
        System.out.println ("after method 3 compltion");
    }
    public void method1 ()
    {
        this.method2 ();
        System.out.println ("after method 2 compltion");
    }
    public static void main (String args[])
    {
        ExceptionPropagation ep = new ExceptionPropagation ();
        ep.method1 ();
        System.out.println ("after method 1 completion");
    }
}

Output:

Exception Propagation Example in Java

As we have not handled the exception in any one of the methods. Let us handle that exception in method 2 and see the output.

class ExceptionPropagation
{
    public void method4 ()
    {
        int a = 10, b = 0;
        int c = a / b;
    }
    public void method3 ()
    {
        this.method4 ();
        System.out.println ("after method 4 compltion");
    }
    public void method2 ()
    {
        try
        {
            this.method3 ();
            System.out.println ("after method 3 compltion");
        }
        catch (Exception e)
        {
            System.out.println (e.getMessage ());
        }
    }
    public void method1 ()
    {
        this.method2 ();
        System.out.println ("after method 2 compltion");
    }
    public static void main (String args[])
    {
        ExceptionPropagation ep = new ExceptionPropagation ();
        ep.method1 ();
        System.out.println ("after method 1 completion");
    }
}

Output:

Exception Propagation in Java with Examples

Re-throwing an Exception in Java:

When an exception occurs in a try block, it is caught by a catch block. This means that the thrown exception is available to the catch block. The following code shows how to re-throw the same exception from the catch block.

Re-throwing an Exception in Java

Suppose there are two classes A and B. If an exception occurs in A, we want to display some message to the user, and then we want to re-throw it. This re-thrown exception can be caught in class B where it can handle. Let us see the following example to understand this concept.

class A
{
    void mathod1 ()
    {
        try
        {
            //take a string with 5 chars. there index
            // will be from 0 to 4
            String str = "hello";
            // exception is thrown in below statement because
            //no index with value 5
            char ch = str.charAt (5);
        }
        catch (StringIndexOutOfBoundsException sie)
        {
            System.out.println ("plz see the index is within the range");
            throw sie;	//re-throwing the exception
        }
    }
}

class B
{
    public static void main (String args[])
    {
        // create an object to A and call method1()
        A a = new A ();
        try
        {
            a.mathod1 ();
        }
        // the re-throw exception is caught by the below catch block
        catch (StringIndexOutOfBoundsException sie)
        {
            System.out.println ("I caught the rethrown exception");
        }
    }
}
Output:

plz see the index is within the range
I caught the rethrown exception

In the next article, I am going to discuss Strings in Java with Examples. Here, in this article, I try to explain Exception Propagation in Java with Examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this Exception Propagation in Java with the Examples article.

Leave a Reply

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