
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Importance of the getCause() method in Java?n
The getCause() method is from Throwable class and we can use this method which returns the cause of the exception or returns null if the cause of the exception is not known. The getCause() method doesn’t accept any arguments and doesn’t throw an exception. It returns the cause that was provided by one of its constructors or that was determined by the formation of the initCause() method of Throwable class.
Syntax
public Throwable getCause()
Example
public class GetCauseMethodTest { public static void main(String[] args) throws Exception { try { myException(); } catch(Exception e) { System.out.println("Cause = " + e.getCause()); } } public static void myException() throws Exception { int arr[] = {1, 3, 5}; try { System.out.println(arr[8]); } catch(ArrayIndexOutOfBoundsException aiobe) { Exception e = new Exception(); throw(Exception); // throwing the exception to be caught by catch block in main() e.initCause(aiobe); // supplies the cause to getCause() } } }
Output
Cause = java.lang.ArrayIndexOutOfBoundsException: 8
- Related Articles
- Importance of yield() method in Java?
- Importance of isDaemon() method in Java?
- Importance of clone() method in Java?
- Importance of the Collectors.filtering() method in Java 9?
- Importance of the parseBoolean() method in Java?\n
- Importance of the accumulate() method of JSONObject in Java?
- Importance of Collectors.flatMapping() method in Java 9?
- Importance of ofInstant() method in Java 9?
- Importance of Thread.onSpinWait() method in Java 9?
- Importance of Optional.or() method in Java 9?
- Importance of join() method in Java?\n
- Importance of destroyForcibly() method in Java 9?
- Importance of transferTo() method of InputStream in Java 9?
- Importance of iterate() method of Stream API in Java 9?\n
- Importance of the JsonPatch interface in Java?

Advertisements