Chained Exception Example
This program demonstrates chained exceptions.
Problem Statement
Write a Java program that chains exceptions using initCause().
Source Code
| 1 | public class ChainedExceptionExample { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Chained Exception Example"); |
| 4 | try { |
| 5 | Exception e=new Exception("Eduinq Primary Exception"); |
| 6 | e.initCause(new ArithmeticException("Eduinq Cause Exception")); |
| 7 | throw e; |
| 8 | } catch(Exception ex) { |
| 9 | System.out.println("Eduinq Exception: "+ex.getMessage()); |
| 10 | System.out.println("Eduinq Cause: "+ex.getCause()); |
| 11 | } |
| 12 | } |
| 13 | } |
Program Output
Eduinq Chained Exception Example Eduinq Exception: Eduinq Primary Exception Eduinq Cause: java.lang.ArithmeticException: Eduinq Cause Exception
Explanation
Chained exceptions link one exception to another. - Provides context about root cause of errors. - Improves debugging and error analysis. - In enterprise systems, chained exceptions help trace complex error flows. Conclusion: Shows chained exceptions with initCause.