Tuesday, November 9, 2021

Exception Propagation in Java Exception Handling

This post shows what is exception propagation in Java and how does exception propagate in case of checked and unchecked exceptions in Java

Exception propagation

When an exceptional condition occurs within a method, the method (where the exception occurred) creates an Exception Object and throws it. The created exception object contains information about the error, its type and the state of the program when the error occurred.

The method where the exception is thrown may handle that exception itself or pass it on. In case it passes it on, run time system goes through the method hierarchy that had been called to get to the current method to search for a method that can handle the exception. This process of going through the method stack is known as Exception propagation in Java.

Note that, if your program is not able to catch any particular exception, that will ultimately be processed by the default handler.

Exception propagation Java example with unchecked Exception

In this example code there are three methods method1, method2 and method3. From method1, method2 is called which in turn calls method3. In method3 there is an operation which will result in an exception. You can see how exception propagates all the way to method1 where it is eventually caught.

public class ExceptionPropagation {

 public static void main(String[] args) {
  ExceptionPropagation exObj = new ExceptionPropagation();
  exObj.method1();
  System.out.println("After handling exception...");
 }
 
 void method3(){
  System.out.println("In method3");
  // This will result in Arithmetic Exception
  // as an attempt is made to divide by zero
  int result = 7/0;  
 }
 
 // This method will forward the exception
 void method2(){
  System.out.println("In method2");
  method3();
 }
 
 // Exception will be handled here
 void method1(){
  try{
   method2();
  } catch(Exception e){
   System.out.println("Exception caught");
  }
 }
}

Output

In method2
In method3
Exception caught
After handling exception
exception propagation in Java
Searching the stack for appropriate Exception Handler

Exception propagation in Java with checked exception

In case of checked exception compiler forces you to put try-catch block so exception has to be handled where it is thrown. If you don't want to do that then you have to declare it in throws clause. Then exception will propagate to the calling method and it should be caught there or it should be declared in throws clause of that method signature too.

public class ExceptionPropagation {

 public static void main(String[] args) {
  ExceptionPropagation exObj = new ExceptionPropagation();
  exObj.method1();
  System.out.println("After handling exception");
 }
 
 // This method declares the exception in throws clause
 void method3() throws FileNotFoundException{
  System.out.println("In method3");
  // throwing exception
  throw new FileNotFoundException();  
 }
 
 // This method also declares the exception in throws clause
 void method2() throws FileNotFoundException{
  System.out.println("In method2");
  method3();
 }
 
 // Exception will be handled here
 void method1(){
  try{
   method2();
  } catch(FileNotFoundException ex){
   System.out.println("FileNotFoundException caught");
  }
 }
}

That's all for this topic Exception Propagation in Java Exception Handling. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. finally Block in Java Exception Handling
  2. Difference Between throw And throws in Java Exception Handling
  3. Try-With-Resources in Java Exception Handling
  4. Best Practices For Exception Handling in Java
  5. Java Exception Handling Interview Questions And Answers

You may also like-

  1. finalize() Method in Java
  2. Java Automatic Numeric Type Promotion
  3. Abstraction in Java
  4. Why Class Name And File Name Should be Same in Java
  5. Lambda Expressions in Java 8
  6. Count Number of Words in a String Java Program
  7. How to Create PDF From XML Using Apache FOP
  8. Difference Between sleep And wait in Java Multi-Threading

No comments:

Post a Comment