Java

Java – Exception Propagation With Examples

If an exception is not handled where it happens, the compiler makes sure that it is handled elsewhere via the process known as exception propagation. For instance, if the main() function invokes a method (method1), and method 1 invokes still another method (method2). The exception is propagated to the method method1 that is calling method2 if the exception occurs in method2 and is not handled there.

Exception Propagation Program in Java

This application shows how Java’s exception propagation works. Here, after dividing an integer num1 by an integer num2, the main function calls a method to output the quotient number.

To compute the quotient value, the printQuotient() function internally calls the calculateQuotient() method.

Since num2 has a value of 0, the exception occurs in the calculateQuotient() function, which is where the division really takes place.

However, since the calculateQuotient() function does not handle exceptions, the exception is immediately sent to the calling method printQuotient(), where it is dealt with.

public class JavaExample {

  //exception occurred in this method but it is not handled here
  //so the exception is propagated to the calling method printQuotient
  int calculateQuotient(int num1, int num2) {
    int temp = num1/num2;
    return temp;
  }

  //Here we have done exception handling
  void printQuotient(int num1, int num2){
    try {
      int q = calculateQuotient(num1, num2);
      System.out.println("Quotient is: " + q);
    }catch(ArithmeticException e){
      System.out.println("Arithmetic exception occurred");
    }
  }
  public static void main(String[] args) {

    int num1 = 10, num2 = 0;

    //calling method
    JavaExample obj = new JavaExample();
    obj.printQuotient(num1, num2);

  }
}

Output:

Arithmetic exception occurred

How can we use this feature to improve the code?

This program demonstrates the operation of Java’s exception propagation. Here, the main function calls a method to output the quotient number after the division of an integer num1 by an integer num2.

The printQuotient() function internally invokes the calculateQuotient() method to get the quotient value.

Since num2 has a value of 0, the calculateQuotient() method, which is where the actual division happens, throws an exception.

The exception is immediately transmitted to the calling method printQuotient(), where it is handled, since the calculateQuotient() function does not handle exceptions.

Exception propagation in checked and unchecked exceptions

Do checked and unchecked exceptions both experience exception propagation? No, verified exceptions do not experience exception propagation; only unchecked exceptions do. The cause is rather straightforward; otherwise, the compiler would raise a compilation error. The checked exceptions must be handled during build time.

Because myMethod3 does not handle the exception, this program throws a compilation problem ().

class JavaExample{
  void myMethod3(){
    throw new java.io.IOException("Checked Exception");
  }
  void myMethod2(){
    myMethod3();
  }
  void myMethod(){
    try{
      myMethod2();
    }catch(Exception e){
      System.out.println("Exception occurred: "+e);
    }
  }
  public static void main(String args[]){
    JavaExample obj=new JavaExample();
    obj.myMethod();
    System.out.println("normal flow");
  }
}

Output:

Error:(3, 5) java: unreported exception java.io.IOException; must 
be caught or declared to be thrown

Related Articles

Leave a Reply

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

Back to top button