为什么你不必处理 `java.lang` 包中 类 可能抛出的异常?
Why don't you have to handle possible exceptions thrown by classes in the `java.lang` package?
所以,在文档中,例如:java.lang.Integer.parseInt
,我注意到代码 header 是:
public static int parseInt(String s) throws NumberFormatException
然而,当一个人有类似 int i = Integer.parseInt(someString);
的语句时,代码可以在没有 try-catch
块的情况下正常编译。
现在,另一方面,如果我用 header:
编写一个方法
public void connectTo(String ip) throws java.net.HostNotFoundException
并调用它而不用适当的 try-catch
块包围调用,编译器将不会拥有它。我并不是说我(或任何人)想要用 try-catch
块包围每个 Integer.parseInt
调用(和其他调用),但我确实很好奇为什么编译器允许它。
因为那些例外是unchecked
来自附件link:
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. Although
this may seem convenient to the programmer, it sidesteps the intent of
the catch or specify requirement and can cause problems for others
using your classes.
因为 NumberFormatException
个子类 IllegalArgumentException
, which in turn subclasses RuntimeException
。
RuntimeException
的子类,与 Exception
的子类不同,不需要显式捕获(尽管它们可以)。
来自 RuntimeException
javadoc 页面:
RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
所以,在文档中,例如:java.lang.Integer.parseInt
,我注意到代码 header 是:
public static int parseInt(String s) throws NumberFormatException
然而,当一个人有类似 int i = Integer.parseInt(someString);
的语句时,代码可以在没有 try-catch
块的情况下正常编译。
现在,另一方面,如果我用 header:
编写一个方法public void connectTo(String ip) throws java.net.HostNotFoundException
并调用它而不用适当的 try-catch
块包围调用,编译器将不会拥有它。我并不是说我(或任何人)想要用 try-catch
块包围每个 Integer.parseInt
调用(和其他调用),但我确实很好奇为什么编译器允许它。
因为那些例外是unchecked
来自附件link:
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. Although this may seem convenient to the programmer, it sidesteps the intent of the catch or specify requirement and can cause problems for others using your classes.
因为 NumberFormatException
个子类 IllegalArgumentException
, which in turn subclasses RuntimeException
。
RuntimeException
的子类,与 Exception
的子类不同,不需要显式捕获(尽管它们可以)。
来自 RuntimeException
javadoc 页面:
RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.