Saturday, 23 May 2015

Exception Propagation in Java exception handling

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.
If your program is not able to catch any particular exception, that will ultimately be processed by the default handler. This process of going through the method stack is known as Exception propagation.

An Example with unchecked Exception

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 exception handling
Searching the stack for appropriate Exception Handler

In case of checked exception compiler forces us to put try-catch block so exception has to be handled where it is thrown. If we don't want to do that then we 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.

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. throws clause
  2. Difference between throw and throws
  3. try-with-resources in Java 7
  4. Best practices for exception handling in Java
  5. Java Exception Handling interview questions

You may also like -

No comments:

Post a Comment