Method
Java Throwable.getCause()
Returns the cause or null if absent or unknown.
Formal Syntax & Structure
java syntaxjava
public Throwable getCause()Working Example
Example Snippetjava
class QuantityException extends Exception { private static final long serialVersionUID = 1L; QuantityException(String message, Throwable cause) { super(message, cause); }}public class Main { static int parse(String text) throws QuantityException { try { return Integer.parseInt(text); } catch (NumberFormatException e) { throw new QuantityException("Quantity must be an integer", e); } } public static void main(String[] args) { try { parse("two"); } catch (QuantityException e) { System.out.println(e.getMessage()); System.out.println(e.getCause().getClass().getSimpleName()); } }}