Printing the Stack Trace of an Exception - Java Object Oriented Design

Java examples for Object Oriented Design:Exception

Description

Printing the Stack Trace of an Exception

Demo Code

public class Main { 
  public static void main(String[] args) {
    try {/*from w w w .j  a  va 2  s  . c  o m*/
      m1();
    }
    catch(MyException e) {      
      e.printStackTrace(); // Print the stack trace
    }
  }
  
  public static void m1() throws MyException {   
    m2();   
  }

  public static void m2() throws MyException {
    throw new MyException("Some error has occurred.");
  }
}
class MyException extends Exception {
  public MyException() {
    super();
  }

  public MyException(String message) {
    super(message);
  }

  public MyException(String message, Throwable cause) {
    super(message, cause);
  }

  public MyException(Throwable cause) {
    super(cause);
  }
}

Related Tutorials