- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Exception chaining in Java
When one exception is chained to the another, it describes the cause of that exception. Constructors of the Throwable class support chained exceptions in Java. They are as follows −
Throwable(Throwable cause) Throwable(String msg, Throwable cause)
The Throwable class has methods which support exception chaining −
Method | Description |
---|---|
getCause() | Returns the original cause of the exception |
initCause(Throwable cause) | Sets the cause for invoking the exception |
Let us see an example of exception chaining in Java.
Example
public class Example { public static void main(String[] args) { try { // creating an exception ArithmeticException e = new ArithmeticException("Apparent cause"); // set the cause of an exception e.initCause(new NullPointerException("Actual cause")); // throwing the exception throw e; } catch(ArithmeticException e) { // Getting the actual cause of the exception System.out.println(e.getCause()); } } }
Output
java.lang.NullPointerException: Actual cause
- Related Articles
- While chaining, can we throw unchecked exception from a checked exception in java?
- Constructor Chaining In Java programming
- What is constructor chaining in Java?
- Exception propagation in Java
- Chained exception in Java\n
- Exception propagation in Java programming
- Difference Between Backward Chaining and Forward Chaining
- How to create a user defined exception (custom exception) in java?
- Method Chaining in JavaScript
- What is exception propagation in Java?
- Null Pointer Exception in Java Programming
- Exception Handling in C++ vs Java
- Is it possible to throw exception without using "throws Exception" in java?
- Chaining comparison operators in C#
- Chaining comparison operators in Python

Advertisements