English 中文(简体)
已检查异常的调用方法
原标题:call method with checked exception

正如我所知,如果方法抛出异常,Java编译器会强制该方法的调用方捕获该异常。

我看到parseInt抛出NumberFormatException

public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);

那么,为什么我可以称之为wthout而不捕获异常:

String str = "5";
int n = Integer.parseInt(str);
最佳回答

由于NumberFormatException扩展了RuntimeException-运行时异常被视为未选中,请参阅JavadocRuntimeException

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

这是Java教程中的一篇文章解释了此功能的含义及其存在原因

问题回答

重要的区别在于,从运行时异常扩展的任何异常都不需要捕获,而任何其他异常都需要捕获。扩展RuntimeException的异常可以在任何时候抛出,例如NullPointerException或ConcurrentModificationException,因此它们不能期望您尝试捕获所有异常。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签