URISyntaxException 在 JDK19 和 JDK20 中的错误消息不同。

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

URISyntaxException different message in JDK19 & JDK20

问题

在JDK 19中 - Caused by: java.net.URISyntaxException: 非法字符在索引7的主机部分:http://www.goo gle.com

在JDK 20中 - Caused by: java.net.URISyntaxException: 非法字符在索引14的主机部分:http://www.goo gle.com

在JDK 19中,异常消息中的索引为7,但在JDK 20中,对于相同的URI,索引为14。

期望异常消息中的索引相同。

英文:

If i am running below code in jdk19 and jdk20 different exception message is coming

public static void main(String[] args) {
        URI uri;
        try {
            uri = new URI("http://www.goo gle.com");
        } catch (URISyntaxException e) {
            System.out.println("Bad URI" + e.getMessage());
            throw new RuntimeException(e);
        }
        System.out.println("Hello World" + uri);

    }

In jdk19 - Caused by: java.net.URISyntaxException: Illegal character in authority at index 7: http://www.goo gle.com

In jdk20 - Caused by: java.net.URISyntaxException: Illegal character in authority at index 14: http://www.goo gle.com

In jdk19 the index is coming as 7 but in jdk20 the index is coming 14 for same URI.

Expecting same index should come in the exception message.

答案1

得分: 0

如果您查看URI构造函数规范,它说明可以抛出URISyntaxException。

您所看到的异常文本是getMessage()调用的结果,它定义了文本的结构。冒号后面是输入字符串,在您的情况下是"http://www.goo gle.com",给定的索引应该是准确的输入字符串的索引。有问题的空格字符位于该字符串的索引14处,因此JDK 20的实现是正确的,而JDK 19错误地将索引指定为7。

但是,依赖异常实例的这些细节通常不是一个好主意。

英文:

If you look at the URI constructor specification, it states that a URISyntaxException can be thrown.

What you see as the exception text, is the result of a getMessage() call, and that defines the structure of the text. After the colon is the input string, in your case "http://www.goo gle.com", and the index given should be an index into exactly that input string. The offending space character is at index 14 in that string, so the JDK 20 implementation is correct, and JDK 19 is wrong in stating the index to be 7.

But, relying on such fine details of exception instances is never a good idea.

huangapple
  • 本文由 发表于 2023年8月10日 12:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872749.html
匿名

发表评论

匿名网友

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

确定