what is Exception chaining / Exception wrapping in java


In this Exception tutorial we will learn what is Exception chaining in java with example and programs.

Contents of page >
  • Exception chaining and other names for it in java?
  • What is Exception chaining/ exception wrapping in java?
  • What Java docs says about Exception chaining in java -

  • Example/ program of Exception chaining/ exception wrapping  in java >
  • Now, let’s understand the stack trace of above exception chaining/ exception wrapping program>



Exception chaining and other names for it in java?
Exception chaining is also known as exception wrapping in java.


What is Exception chaining/ exception wrapping in java?
When Exception is thrown we catch it and throws some other Exception than the concept is called Exception chaining in java.

What Java docs says about Exception chaining in java -
‘An application often responds to an exception by throwing another exception. In effect, the first exception causes the second exception. It can be very helpful to know when one exception causes another. Chained Exceptions help the programmer do this.’

Example/ program of Exception chaining/ exception wrapping  in java >
package exceptionChainingExamplePkg;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ExceptionChainingExample {
   public static void main(String[] args) {
          new ExceptionChainingExample().method1();
   }
   void method1() {
          method2();
   }
  
   void method2() {
          method3();
   }
  
   void method3() {
          try{
                 throw new NullPointerException();
          }catch(RuntimeException e){
                 throw new RuntimeException("RuntimeException occured", e);
          }
   }
}


Output/StackTrace of above Exception chaining/ exception wrapping java example -
Exception in thread "main" java.lang.RuntimeException: RuntimeException occured
   at exceptionChainingExamplePkg.ExceptionChainingExample.method3(ExceptionChainingExample.java:21)
   at exceptionChainingExamplePkg.ExceptionChainingExample.method2(ExceptionChainingExample.java:14)
   at exceptionChainingExamplePkg.ExceptionChainingExample.method1(ExceptionChainingExample.java:10)
   at exceptionChainingExamplePkg.ExceptionChainingExample.main(ExceptionChainingExample.java:6)
Caused by: java.lang.NullPointerException
   at exceptionChainingExamplePkg.ExceptionChainingExample.method3(ExceptionChainingExample.java:19)
   ... 3 more


Let’s understand the above example/program in java -
When NullPointerException is thrown we catch it and a new RuntimeException exception is created with the original cause(i.e. NullPointerException ) attached and the chain of exceptions is thrown up to the next higher level exception handler in java.




Now, let’s understand the stack trace of above exception chaining/ exception wrapping program>
RuntimeException was thrown in program.
Now, topmost ‘at’ tells the method which generated/throwed the exception.
method3() throwed exception.
method3() was called from method2()
method2() was called from method1()
method1() was called from main method.

Below mentioned is Caused by: which tells you the actual cause of Exception. So the root cause was NullPointerException.


Summary >
So in this Exception handling tutorial we learned Exception chaining and other names for it in java? What is Exception chaining/ exception wrapping in java? What Java docs says about Exception chaining in java. Example/ program of Exception chaining/ exception wrapping  in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


Related >
checked, unchecked Exceptions and Error>





5 Exceptions handling keywords - try, catch, finally, throw and throws >










Automatic Resource Management and Try-with-resources in java 7 >




Exception propagation>





Method overloading on basis of exceptions



Must read for you :

No comments:

Post a Comment