-1

我知道出错的行是to_return = find(list,false);How can I get this line of the line of the line when there is NullPointerExceptiontype error? 还是一般的行号?

我尝试了几件事。最接近的是这个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;
    }
}
4

2 回答 2

0
  1. 如果您只想要当前堆栈跟踪,请使用Thread.currentThread().getStackTrace()

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

于 2013-07-31T09:27:31.480 回答
0

在 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 在“将行号属性添加到生成的类文件”处标记了复选框。

于 2018-07-06T16:11:28.950 回答