A simple Java program demonstrating methods of Exception class
Output of the program :
public class ExceptionMethodsDemo { public static void first() { try { System.out.println("In try block : "); throw new Exception("New Exception"); } catch(Exception e) { System.out.println("In catch block : "); System.out.println("getMessage() : " + e.getMessage()); System.out.println("getLocalizedMessage() : " + e.getLocalizedMessage()); System.out.println("toString() : " + e.toString()); System.out.println("getClass().getName() : " + e.getClass().getName()); System.out.println("getCause() : " + e.getCause()); int i = 1; for(StackTraceElement ste : e.getStackTrace()) { System.out.println("e.getStackTrace() : method " + i +" "+ ste.getMethodName()); i++; } System.out.println("printStackTrace() : "); e.printStackTrace(System.out); } } public static void second() { first(); } public static void third() { second(); } public static void main(String[] args) { third(); } }
Output of the program :