public class ExceptionLineNumber {
  public static void main(String[] args) throws Exception {    try {
      throw new java.lang.RuntimeException("ABC");
    }
    catch (Exception ex) {
      StackTraceElement[] ste = ex.getStackTrace();
      String className = ExceptionLineNumber.class.getName();
      for (int i = 0, len = ste.length; i < len; i++) {
        if (className.equals(ste[i].getClassName()))
          System.out.println(
              "  类:" + ste[i].getClassName() +
              "  方法:" + ste[i].getMethodName() +
              "  行号:" + ste[i].getLineNumber() +
              "\n");
      }
    }
  }
}  类:ExceptionLineNumber  方法:main  行号:5

解决方案 »

  1.   

    请问各位老大:
    下面这个文件我编译后正常,但是运行时提示错误:
    D:\>javac ExceptionLineNumber.javaD:\>java ExceptionLineNumber
    Exception in thread "main" java.lang.NoClassDefFoundError: ExceptionLineNumber
    是什么原因呢?public class ExceptionLineNumber {
      public static void main(String[] args) throws Exception {    try {
          throw new java.lang.RuntimeException("ABC");
        }
        catch (Exception ex) {
          StackTraceElement[] ste = ex.getStackTrace();
          String className = ExceptionLineNumber.class.getName();
          for (int i = 0, len = ste.length; i < len; i++) {
            if (className.equals(ste[i].getClassName()))
              System.out.println(
                  "  类:" + ste[i].getClassName() +
                  "  方法:" + ste[i].getMethodName() +
                  "  行号:" + ste[i].getLineNumber() +
                  "\n");
          }
        }
      }
    }