Chained exceptions in Java: Type of constructors and methods

By using the process of chained exception, one exception can be connected to another exception. This second exception is the reason for the occurrence of the first exception. In some situations an exception can be a two step process, say first exception happened but it happened due to the second exception. There are two types of constructors to handle chained exceptions:

Type-1: Exception happening because of causeExc


Throwable(Throwable causeExc)

Type-2: Exception happening because of causeExc and also there is a description of the exception


Throwable(String msg, Throwable causeExc)

Methods used by chained exceptions:

  • Throwable getCause( ): It is used to return exception underlying current exception. Null is returned, if there’s no underlying exception.
  • Throwable initCause(Throwable causeExc): It is used to associate causeExc with calling exception. It used to add the chain of exceptions.

Leave a Reply