Using chained exception in Java you can associate one exception with another. Chained exception helps in the scenario where one exception causes another exception. The exception that will be thrown is the second exception in such scenario but you can set the cause of original exception in the second exception so in a way you are chaining the exceptions.
For example, you are writing a class where two command line arguments are passed and then you convert those arguments to integer. If you don't pass two arguments that will result in ArrayIndexOutOfBoundsException but the real cause here is not having enough arguments. So you can set the cause as IllegalArgumentException with the appropriate message.
Constructors and Methods for chained exception in Java
In Throwable class there are two constructors that support chained exception.
- Throwable(Throwable expObj)– Here expObj signifies the exception that caused the current exception.
- Throwable(String msg, Throwable expObj)– With this constructor you can also pass an appropriate message along with the exception.
In Throwable class there are two methods that support chained exception.
- Throwable getCause()- This method returns the underlying exception that caused the current exception.
- Throwable initCause(Throwable)– This method sets the current exception's cause. The Throwable argument to initCause is the exception that caused the current exception.
Example Java code demonstrating chained exception
Let’s write Java code for the example already stated above. In the code you expect two arguments are passed and then those arguments are converted to integer.
public class ChainedExpDemo { public static void main(String[] args) { ChainedExpDemo ced = new ChainedExpDemo(); try{ ced.divide(args); }catch(Exception ex){ System.out.println("Caught - " + ex); System.out.println("Original Cause " + ex.getCause()); } } private void divide(String[] args){ try{ int num1 = Integer.parseInt(args[0]); int num2 = Integer.parseInt(args[1]); double result = num1/num2; }catch(ArrayIndexOutOfBoundsException ex){ if(args.length != 2){ ex.initCause(new IllegalArgumentException("Two parameters should be passed")); } throw ex; } } }
Output when the code is executed without passing any argument-
Caught - java.lang.ArrayIndexOutOfBoundsException: 0 Original Cause java.lang.IllegalArgumentException: Two parameters should be passed
Real usage of chained exception
Real usage of chained exception in Java is in the case when you don’t want to propagate layer specific exception to another layer but still want to retain the original exception. This is also one of the best practice for exception handling. As example– If SQLException is caught in your DB layer, there is no sense in letting the same exception propagate to your business layer. In this case best thing to do is catch the SQLException (checked exception) in your DAO layer and rethrow another exception (unchecked) that should propagate to business layer. You should send the original exception instance as a parameter.catch(SQLException ex){ throw new RuntimeException("Error in DB layer", ex); }Reference : https://docs.oracle.com/javase/tutorial/essential/exceptions/chained.html
That's all for the topic Chained Exception in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment