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. {  
  3. void methodC()  
  4. {  
  5. int error=1000/0; // Exception generation  
  6. }  
  7. void methodB()  
  8. {  
  9. methodC();  
  10. }  
  11. void methodA()  
  12. {  
  13. try  
  14. {  
  15. methodB();  
  16. }  
  17. catch(Exception e)  
  18. {  
  19. System.out.println("Exception is handled here");  
  20. }  
  21. }  
  22. public static void main(String args[])  
  23. {  
  24. Test t=new Test();  
  25. t.methodA();  
  26. System.out.println("working in normal flow");  
  27. }  

Output:

Exception is handled here
working in normal flow

In the preceding example, the exception occurred in methodC 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. {  
  3. void methodC()  
  4. {  
  5. throw new java.io.IOException("error occurred"); //checked exception  
  6. }  
  7. void methodB()  
  8. {  
  9. methodC();  
  10. }  
  11. void methodA()  
  12. {  
  13. try  
  14. {  
  15. methodB();  
  16. }  
  17. catch(Exception e)  
  18. {  
  19. System.out.println("Exception is handled here");  
  20. }  
  21. }  
  22. public static void main(String args[])  
  23. {  
  24. Test t=new Test();  
  25. t.methodA();  
  26. System.out.println("working in normal flow");  
  27. }  

 

Output:

Compile Time Error

The preceding situation occurs because of a checked exception that is not forwarded in the calling chain.

Up Next
    Ebook Download
    View all
    Learn
    View all