Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

A simple Java program demonstrating methods of Exception class

A simple Java program demonstrating methods of Exception class

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 :




 
© 2021 Learn Java by Examples Template by Hubberspot