Java的getStacktrace返回错误的行号。

huangapple go评论93阅读模式
英文:

Java getStacktrace returns the wrong line number

问题

*e.getStackTrace()[0].getLineNumber()*无法按预期工作。

给定异常

org.xml.sax.SAXParseException; lineNumber: 61; columnNumber: 9; Auf Elementtyp "Test" müssen entweder Attributspezifikationen, ">" oder "/>" folgen。
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)

我想获取第一行的lineNumber和columnNumber(61和9)。
我尝试了

e.getStackTrace()[0].getLineNumber();

来获取最近异常的行号。然而,在这种情况下,它返回的是257而不是61。

我通过以下代码片段采用蛮力的方式提取了所需的行号:

StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
String str = errors.toString();
Matcher m = Pattern.compile("[^0-9]([0-9]+)[^0-9]([0-9]+).*").matcher(str);
if (m.matches()) {
line = Integer.parseInt(m.group(1));
pos = Integer.parseInt(m.group(2));
}
是否有更好的方法来实现所需的行为,最重要的是:
为什么不返回最近异常的行号,而是另一个行号?

英文:

TL;DR: e.getStackTrace()[0].getLineNumber() does not work as expected.

Given the exception

org.xml.sax.SAXParseException; lineNumber: 61; columnNumber: 9; Auf Elementtyp "Test" müssen entweder Attributspezifikationen, ">" oder "/>" folgen.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)

I want to get the lineNumber and columnNumber of the first line (61 and 9).
I tried

e.getStackTrace()[0].getLineNumber();

to get the lineNumber of the most recent exception. In this case, however, it returns 257 instead of 61.

I've managed to extract the desired line number by brute force with the following snippet:

StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
String str = errors.toString();
Matcher m = Pattern.compile("[^0-9]*([0-9]+)[^0-9]*([0-9]+).*").matcher(str);
if (m.matches()) {
	line = Integer.parseInt(m.group(1));
	pos = Integer.parseInt(m.group(2));
}

Is there a better way to achieve the desired behavior, and most of all:
Why is not the line number of the most recent exception returned, but another one?

答案1

得分: 1

这将返回正确的行号。只是不是你正在寻找的行号。

e.getStackTrace()[0].getLineNumber();

这将返回异常抛出的Java源代码中的行号。这不是解析失败的XML的行号。

这个堆栈跟踪元素,因此它的行号,是你堆栈跟踪中的第一个 at 行:

at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)

如果你想要XML的行号(和列号),你需要使用 e.getLineNumber()(和 e.getColumnNumber()),假设 e 是类型为 SAXParseException 的引用,否则你需要先进行类型转换。

当尝试从异常中获取特定数据时,最好先阅读API文档以查看是否公开了该数据(在 SAXParseException 的情况下是公开的),而不是立刻使用正则表达式等工具。

另外,如果你确实需要从异常消息中提取数据,我建议使用 e.getMessage() 而不是打印整个堆栈跟踪。

英文:

It returns the right line number. It is just not the line number you're looking for.

e.getStackTrace()[0].getLineNumber();

This will return the line number in the Java source code where the exception is thrown. It is not the line number of the XML where parsing failed.

This stacktrace element, and thus its line number, is the first at line in your stacktrace:

at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)

If you want the line (and column) of the XML, you need to use e.getLineNumber() (and e.getColumnNumber()), assuming e is a reference of type SAXParseException, otherwise you will need to cast first.

When trying to get specific data from an exception, it is always a good to read the API documentation to see if that data is exposed (which it is in the case of SAXParseException), before resorting to tools like regular expressions.

As an aside, if you do want or need to extract data from an exception message, I would recommend to use e.getMessage() instead of printing the entire stacktrace.

答案2

得分: 0

似乎第一行中的 lineNumbercolumnNumber 与您尝试解析的 XML 中的位置相关(请参阅 SAXParseException 构造函数)。

英文:

It seems like lineNumber and columnNumber in the first line relate to a position in the XML you're trying to parse (see SAXParseException constructor)

huangapple
  • 本文由 发表于 2020年7月22日 19:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63033269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定