Chained exception in Javan

Chained exception helps to relate one exception to other. Often we need to throw a custom exception and want to keep the details of an original exception that in such scenarios we can use the chained exception mechanism. Consider the following example, where we are throwing a custom exception while keeping the message of the original exception.


Live Demo

public class Tester {
   public static void main(String[] args) {
      try {
      }catch(ApplicationException e) {          

   public static void test() throws ApplicationException {
      try {
         int a = 0;
         int b = 1;
      }catch(Exception e) {
         throw new ApplicationException(e);

class ApplicationException extends Exception {
   public ApplicationException(Exception e) {          


java.lang.ArithmeticException: / by zero

The throwable class supports chained exception using the following methods:


  • Throwable(Throwable cause) - the cause is the current exception.

  • Throwable(String msg, Throwable cause) - msg is the exception message, the cause is the current exception.


  • getCause - returns actual cause.

  • initCause(Throwable cause) - sets the cause for calling an exception.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know