Exception chaining in java allows us to relate one exception to another exception. 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.
The Throwablehas below methods that support exception chaining.
1 2 3 4 |
Throwable getCause() Throwable initCause(Throwable) Throwable(String, Throwable) Throwable(Throwable) |
You can pass Throwable object to another Throwable constructor or the initCause() method, so that the original exception is propagated to other exception.
Exception Chaining in Java Example
In the below example we have a method getBasicSalary() which throws an MultiplyZeroException. We have another method getEmployeeTotalSalary() which uses getBasicSalary() and catches the exception. But getEmployeeTotalSalary() throws another exception. While throwing the exception we chaining it with the old exception. So when we try to print the exception and its cause we get the entire chained exception details. Also we have printed the stack trace which also give the chained exception details.
WrongSalaryException and MultiplyZeroException are the user defined exceptions,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.kscodes.test; import com.kscodes.exception.MultiplyZeroException; import com.kscodes.exception.WrongSalaryException; public class ExceptionChainingExample { public static void main(String args[]){ ExceptionChainingExample example = new ExceptionChainingExample(); try { example.getEmployeeTotalSalary(0, 0, 100); } catch (WrongSalaryException e) { System.out.println(e); System.out.println(e.getCause()); e.printStackTrace(); } } public int getEmployeeTotalSalary(int days, int perDaySal, int bonus) throws WrongSalaryException { try { return getBasicSalary(days, perDaySal) + bonus; } catch (MultiplyZeroException mze) { throw new WrongSalaryException("We cannot give this Employee Salary", mze); } } private int getBasicSalary(int days, int perDaySal) throws MultiplyZeroException { if (days == 0 && perDaySal == 0) { throw new MultiplyZeroException("Cannot multiple two zero numbers"); } return days * perDaySal; } } |
Output
1 2 3 4 5 6 7 8 9 10 11 |
com.kscodes.exception.WrongSalaryException: We cannot give this Employee Salary com.kscodes.exception.MultiplyZeroException: Cannot multiple two zero numbers com.kscodes.exception.WrongSalaryException: We cannot give this Employee Salary at com.kscodes.test.ExceptionChainingExample.getEmployeeTotalSalary(ExceptionChainingExample.java:25) at com.kscodes.test.ExceptionChainingExample.main(ExceptionChainingExample.java:11) Caused by: com.kscodes.exception.MultiplyZeroException: Cannot multiple two zero numbers at com.kscodes.test.ExceptionChainingExample.getBasicSalary(ExceptionChainingExample.java:32) at com.kscodes.test.ExceptionChainingExample.getEmployeeTotalSalary(ExceptionChainingExample.java:23) ... 1 more |