【问题标题】:How to get the line number in error in java?如何在java中获取错误的行号?
【发布时间】:2012-07-10 01:29:11
【问题描述】:

我知道出错的行是to_return = find(list,false); 当出现NullPointerException 类型的错误时,如何获取该行的行号?还是一般的行号?

我尝试了几件事。最接近的是这个Called.getLineNumber(),它给了我StackTraceElement Called = new Throwable().fillInStackTrace().getStackTrace()[0];的行号

public TestObject[] myfind(Subitem list )throws Exception{
    TestObject[]to_return=null;
    try {
        to_return = find(list,false);
    }
    catch (RationalTestException ex) {
        //logStoreException(ex);
        StackTraceElement Called = new Throwable().fillInStackTrace().getStackTrace()[0];
        StackTraceElement Calling = new Throwable().fillInStackTrace().getStackTrace()[1];
        throw new Exception (this.add_debugging_info(Called, Calling, ex.getMessage()));

    }   
    catch (NullPointerException npe) {
        StackTraceElement Called = new Throwable().fillInStackTrace().getStackTrace()[0];
        StackTraceElement Calling = new Throwable().fillInStackTrace().getStackTrace()[1];
        logStoreException(npe);
        System.out.println("Line number: "+npe.getStackTrace()[0].getLineNumber()); 
        System.out.println("Line number2: "+Integer.toString(Called.getLineNumber()));
        System.out.println(this.add_debugging_info(Called, Calling, npe.getMessage()));
        throw new Exception (this.add_debugging_info(Called, Calling, npe.getMessage()));
    }   
    catch (Exception ex) {
        StackTraceElement Called = new Throwable().fillInStackTrace().getStackTrace()[0];
        StackTraceElement Calling = new Throwable().fillInStackTrace().getStackTrace()[1];
        throw new Exception (this.add_debugging_info(Called, Calling, ex.getMessage()));

    } 
    finally {
        //unregisterAll();
        //unregister(to);
        return to_return;
    }
}

【问题讨论】:

  • 如果你在可观察性的情况下编译,行号应该列在异常回溯中。
  • 您的代码正在创建一个异常并询问该异常是在哪里创建的。要获取真正异常的行号,您需要使用真正的异常,而不是您创建的异常。
  • (即,在ex 上调用getStackTrace。)(并且不要调用fillInStackTrace,因为这会覆盖捕获的异常中的信息。)
  • 有什么原因你不能只做npe.printStackTrace();/为什么你的运行时代码需要有行号?如果您只是希望行号信息通过您重新抛出的Exception 传播,您可以随时执行throw new Exception(npe);,这将保留有关原始Exception 的所有详细信息。
  • (并且 printStackTrace 可以正常工作,如果您使用 原始 异常而不是您创建的异常调用它。您知道 ExceptionThrowable 的子类,对吧?)

标签: java error-handling


【解决方案1】:
  1. 如果您只需要当前堆栈跟踪,请使用Thread.currentThread().getStackTrace()

  2. 如果您想强制 JVM 填充堆栈跟踪,请在您的 JVM 上设置选项-XX:-OmitStackTraceInFastThrow

【讨论】:

    【解决方案2】:

    在 Eclipse 中运行时,要获取行号本身,您需要获取 StackTrace 数组并在其上调用 getLineNumber()。

    以下内容在我的 Utils 类中为我工作,isSessionConnectible 方法:

    ... catch (ClassFormatError cfe) {
            logger.error("Problem ClassFormatError connecting. " + cfe.getMessage() + " " + cfe.getCause());
            int size = cfe.getStackTrace().length - 1;
            logger.error("   Root cause: " + cfe.getStackTrace()[size].getMethodName() + " " + cfe.getStackTrace()[size].getClassName());
            if (size>1) { 
                logger.error("   Penultimate cause: method=" + cfe.getStackTrace()[size-1].getMethodName() + " class=" + cfe.getStackTrace()[size-1].getClassName() + 
                        " line=" + cfe.getStackTrace()[size-1].getLineNumber()); 
            }
    

    抛出时的结果:

    2018-07-06 12:00:12 ERROR Utils:319 - Problem ClassFormatError connecting to Hibernate. Absent Code attribute in method that is not native or abstract in class file javax/transaction/SystemException null
    2018-07-06 12:00:12 ERROR Utils:322 -    Root cause: main <mypackage>.Utils
    2018-07-06 12:00:12 ERROR Utils:324 -    Penultimate cause: method=isSessionConnectible class=<mypackage>.Utils line=306
    

    顺便说一句,在 Eclipse 中,确保 Window --> Preferences --> Java --> Compiler 在“将行号属性添加到生成的类文件”处标记了复选框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 2016-08-24
      • 2017-10-04
      相关资源
      最近更新 更多