package com.example; public class CustomException extends Exception { public CustomException(String message, Throwable cause) { super(message, cause); } } public class Main { public static void main(String[] args) { try { throw new CustomException("Custom exception thrown", new NullPointerException("NullPointerException")); } catch (CustomException e) { System.out.println("Exception cause: " + e.getCause()); } } }
package com.example; public class Main { public static void main(String[] args) { try { Integer.parseInt("abc"); } catch (Exception e) { System.out.println("Exception cause: " + e.getCause()); } } }Here, we are using the built-in exception NumberFormatException by trying to parse a non-integer string using Integer.parseInt(). Then, we are catching the exception and using getCause() to print the cause of the exception. Package/Library: The getCause() method is a part of the java.util package.