Exception Propagation in Java

Exception Propagation in Java

 
In this article, we will explain the propagation of the exceptions following some basic examples for good learning.
 

Exception

 
An “exception” is an event that occurs during the execution of a program that disrupts the normal flow of the program. When this error occurs within a method then an object is created and is handed off to the runtime system. The object created is called an “exception object” that contains the information about the error, its type and state of the program when the error occurred. The process of creating an object and handing it to the runtime system is known as “throwing" the exception.
 

Exception Propagation

 
When the exception occurs and is thrown to the runtime system, it is first thrown from the top of the stack to be caught and if not caught then it drops down the call stack to the previous method and if again it is not caught then it drops down to the previous methods and so on. This process will go until they are caught or until they reach the bottom of the call stack. This is done using the call stack in which methods calls propagate in series. This process can be shown by the following figure.
  • Method C <------ Exception Occurred here
  • Method B
  • Method A <------ Exception Handler Written Here
  • main()
Basically exception propagation is held in the following two ways:
  • Checked Exception
  • Unchecked Exception
 
Example of Exception propagation: Unchecked Exception
 
In the calling chain of method calls, unchecked exceptions are forwarded by default.
  1. class Test {  
  2.  void methodC() {  
  3.   int error = 1000 / 0// Exception generation    
  4.  }  
  5.  void methodB() {  
  6.   methodC();  
  7.  }  
  8.  void methodA() {  
  9.   try {  
  10.    methodB();  
  11.   } catch (Exception e) {  
  12.    System.out.println("Exception is handled here");  
  13.   }  
  14.  }  
  15.  public static void main(String args[]) {  
  16.   Test t = new Test();  
  17.   t.methodA();  
  18.   System.out.println("working in normal flow");  
  19.  }  
  20. }  
Output:
 
Exception is handled here  
working in normal flow
 
In the preceding example, the exception occurred in methods where it is not handled, so it drops down to the previous method in the call stack where it is also not handled then again that same process is done and finally the exception is handled in methodA.
 
Example of Exception propagation: checked Exception
 
In the calling chain of method calls, unchecked exceptions are “not” forwarded by default.
  1. class Test {  
  2.  void methodC() {  
  3.   throw new java.io.IOException("error occurred");  
  4.  }  
  5.  void methodB() {  
  6.   methodC();  
  7.  }  
  8.  void methodA() {  
  9.   try {  
  10.    methodB();  
  11.   } catch (Exception e) {  
  12.    System.out.println("Exception is handled here");  
  13.   }  
  14.  }  
  15.  public static void main(String args[]) {  
  16.   Test t = new Test();  
  17.   t.methodA();  
  18.   System.out.println("working in normal flow");  
  19.  }  
  20. }  
Output:
Compile Time Error
  
The preceding situation occurs because of a checked exception that is not forwarded in the calling chain.


Similar Articles