0

我从设置了异常断点的 IDE 启动 Grails 2.1.5 应用程序NumberFormatException(这意味着只要抛出此异常,应用程序就会中断)。

如果我随后从 Grails 控制台执行以下代码:

import  groovy.util.*
import groovyx.net.http.*

def uri = 'http://ws.audioscrobbler.com/2.0/?artist=Yelle&mbid=f43d43c8-eedf-4628-99b0-04120e7124c8&method=artist.gettopalbums&api_key=6e331f856413a5e3dfc91ec41cea5415&limit=6'

XmlSlurper().parse(uri)

异常断点是因为下面的代码触发的Long.parseLong

public static long parseLong(String s, int radix)
          throws NumberFormatException
{
    if (s == null) {
        throw new NumberFormatException("null");
    }

    // rest of method omitted
}

然而,它似乎XmlSlurper().parse(uri)返回了预期值,所以我猜这个异常是在某个地方处理的,但我不知道在哪里。我对为什么Long.parseLong第一个参数使用空值调用感到困惑。这是一个错误XmlSlurper还是只是一些奇怪的实现细节?

更新

根据要求,这是调用堆栈。我正在使用 JDK 7 和 Groovy 1.8.8。我尝试自己调试它,但如您所见,我缺少很多相关的源文件。

  at java.lang.Long.parseLong(Long.java:404)
  at java.lang.Long.parseLong(Long.java:483)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1571)
  at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source:-1)
  at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source:-1)
  at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source:-1)
  at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source:-1)
  at org.apache.xerces.parsers.XMLParser.parse(Unknown Source:-1)
  at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source:-1)
  at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source:-1)
  at groovy.util.XmlSlurper.parse(XmlSlurper.java:146)
  at groovy.util.XmlSlurper.parse(XmlSlurper.java:212)
4

1 回答 1

4

据我所知,OpenJDK mercurial 存储库中的这个文件是 Java 7 update 25 中的版本,sun.net.www.protocol.http.HttpURLConnection相关的摘录是

 1570     try {
 1571         cl = Long.parseLong(responses.findValue("content-length"));
 1572     } catch (Exception exc) { };

因此,NumberFormatException只要 HTTP 响应没有Content-Length标头,就会抛出(并立即忽略)。

于 2013-07-31T10:33:35.240 回答