Getting the Stack Trace of an Exception

 
public class Main {
  public static void main(String[] argv) {

    try {
      throw new Exception("test");
    } catch (Throwable e) {
      StackTraceElement stack[] = e.getStackTrace();

      for (int i = 0; i < stack.length; i++) {
        String filename = stack[i].getFileName();
        if (filename == null) {
          System.out.println("The source filename is not available"); 
        }
        String className = stack[i].getClassName();
        System.out.println("class name:"+className);
        String methodName = stack[i].getMethodName();
        System.out.println("methodName"+methodName);
        boolean isNativeMethod = stack[i].isNativeMethod();
        int line = stack[i].getLineNumber();
      }
    }
  }
}
  
Home 
  Java Book 
    Runnable examples