java 如何在java中获取错误的行号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11405455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the line number in error in java?
提问by Radek
I know that the line in error is to_return = find(list,false);
How can I get the line number of this line when there is NullPointerException
type of error? Or in line number in general?
我知道错误的行是to_return = find(list,false);
当出现NullPointerException
错误类型时如何获取该行的行号?还是一般的行号?
I tried few things. The closest is this one Called.getLineNumber()
which gives me the line number of StackTraceElement Called = new Throwable().fillInStackTrace().getStackTrace()[0];
我尝试了几件事。最接近的是这个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;
}
}
回答by mauhiz
If you just want the current stack trace, use
Thread.currentThread().getStackTrace()
If you want to force the JVM to fill in the stack traces, please set the option
-XX:-OmitStackTraceInFastThrow
on your JVM.
如果您只想要当前的堆栈跟踪,请使用
Thread.currentThread().getStackTrace()
如果您想强制 JVM 填充堆栈跟踪,请
-XX:-OmitStackTraceInFastThrow
在您的 JVM 上设置该选项。
回答by Tihamer
When running in Eclipse, to get the line number itself, you need to get the StackTrace array and call getLineNumber() on it.
在 Eclipse 中运行时,要获取行号本身,您需要获取 StackTrace 数组并对其调用 getLineNumber()。
The following worked for me in my Utils class, isSessionConnectible method:
以下在我的 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());
}
Result when thrown:
抛出时的结果:
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
BTW, in Eclipse, make sure that Window --> Preferences --> Java --> Compiler has the checkbox marked at "Add line number attributes to generated class files".
顺便说一句,在 Eclipse 中,确保 Window --> Preferences --> Java --> Compiler 在“将行号属性添加到生成的类文件”中标记了复选框。