Pin It

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 :




Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot