Exception chaining in Java

Exception chaining is very powerful feature supported by the JDK but most of the developers are not able to use the advantage of Exception chaining in their application. Most of the developers have also seen the code in their existing project but unable to identify the advantage of exception chaining.

Definition: When one exception causes another exception then the second exception must print the log of the first exception also.

Exception Chaining
Exception Chaining

Consider below example:

package com.G2.Exception;

public class ExceptionTest {

	static int arr[] = { 11, 22, 33 };

	public static void main(String[] args) {

		try {
			System.out.println(arr[4]);
		} catch (ArrayIndexOutOfBoundsException ae) {
			try {
				MyCustomException e = new MyCustomException("Exception Occured");
				throw e;

			} catch (MyCustomException e) {
				e.printStackTrace();
			}
		}
	}
}

class MyCustomException extends Exception {
	public MyCustomException(String msg) {
		super(msg);
	}
}

Output :

com.G2.Exception.MyCustomException: Exception Occured
at com.G2.Exception.ExceptionTest.main(ExceptionTest.java:13)

You can see the poor programming practice from the above java code.
Actual reason of the exception is “ArrayIndexOutOfBoundsException“ but it is printing the logs of the last exception.
A cause can be associated with a throwable in two ways:  via a constructor that takes the cause as an argument, or via the initCause(Throwable) method.
Solution 1:
First solution is that, we can use the method initcause() Method of class Throwable to retain the cause of the exception.

package com.G2.Exception;

public class ExceptionTest {

	static int arr[] = { 11, 22, 33 };

	public static void main(String[] args) {

		try {
			System.out.println(arr[4]);
		} catch (ArrayIndexOutOfBoundsException ae) {
			try {
				MyCustomException e = new MyCustomException("Exception Occured");
				e.initCause(ae);
				throw e;

			} catch (MyCustomException e) {
				e.printStackTrace();
			}
		}
	}
}

class MyCustomException extends Exception {
	public MyCustomException(String msg) {
		super(msg);
	}
}

Output:

com.G2.Exception.MyCustomException: Exception Occured
at com.G2.Exception.ExceptionTest.main(ExceptionTest.java:13)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 4
at com.G2.Exception.ExceptionTest.main(ExceptionTest.java:10)

Solution 2:

In the constructor of the MyCustomException, pass the object of the Throwable.

package com.G2.Exception;

public class ExceptionTest {

	static int arr[] = { 11, 22, 33 };

	public static void main(String[] args) {

		try {
			System.out.println(arr[4]);
		} catch (ArrayIndexOutOfBoundsException ae) {
			try {
				MyCustomException e = new MyCustomException("Exception Occured",ae);
				throw e;

			} catch (MyCustomException e) {
				e.printStackTrace();
			}
		}
	}
}

class MyCustomException extends Exception {
	public MyCustomException(String msg,Throwable t) {
		super(msg,t);
	}
}

Following methods are supoorted by the java to support the exception chaining:

Throwable getCause()
Throwable initCause(Throwable)
Throwable(String, Throwable)
Throwable(Throwable)
Throwable printStackTrace();

Posted

in

by

Tags:


Related Posts

Comments

4 responses to “Exception chaining in Java”

  1. […] Read – Read Exception chaining . Sharevar dzone_url = […]

  2. […] 13. What is Exception chaining ? Answer : When one exception causes another exception then the second exception must print the log of the first exception also. Read more.. […]

  3. sudhanshu Avatar
    sudhanshu

    Great Example with explanation. Thanks

  4. Hanumant Bandgar Avatar
    Hanumant Bandgar

    nice oneeeeeeeeeeee ggggggggreat…………..

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading