gpt4 book ai didi

java - 为什么 Integer.parseInt 在没有 try catch 的情况下进行编译?

转载 作者:搜寻专家 更新时间:2023-11-01 04:02:04 26 4
gpt4 key购买 nike

据我所知,在 Java 中抛出异常的函数不应在没有 try 和 catch 或在其上方的函数中减速的情况下进行编译。那这段代码怎么就合法了又不被碾压呢?

public static void main(String[] args) {
Integer.parseInt("33");
}

即使 Integer.parseInt()抛出:NumberFormatException - 如果字符串不包含可解析的整数。

最佳答案

NumberFormatException 扩展了 RuntimeException,这是一个不需要捕获的未经检查的异常。

enter image description here

摘自 Java 教程

Because the Java programming language does not require methods to catch or to specify unchecked exceptions (RuntimeException, Error, and their subclasses), programmers may be tempted to write code that throws only unchecked exceptions or to make all their exception subclasses inherit from RuntimeException. Both of these shortcuts allow programmers to write code without bothering with compiler errors and without bothering to specify or to catch any exceptions. Full Article

NumberFormatException Api Docs

关于java - 为什么 Integer.parseInt 在没有 try catch 的情况下进行编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14255880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com