Java 什么时候在方法签名中使用关键字抛出?
我正在阅读Helbert Schildt的Java Complete Reference,其中他使用异常展示了这个示例Java 什么时候在方法签名中使用关键字抛出?,java,exception,Java,Exception,我正在阅读Helbert Schildt的Java Complete Reference,其中他使用异常展示了这个示例 class ThrowDemo { static void demoproc() { try { throw new NullPointerException("demo"); } catch(NullPointerException e) { System.out.println("
class ThrowDemo {
static void demoproc() {
try {
throw new NullPointerException("demo");
} catch(NullPointerException e) {
System.out.println("Caught inside demoproc.");
throw e; // rethrow the exception
}
}
public static void main(String args[]) {
try {
demoproc();
} catch(NullPointerException e) {
System.out.println("Recaught: " + e);
}
}
}
为什么方法
demoproc()
的签名中没有关键字抛出
,而它可能在catch子句中抛出异常 NullPointerException
是。只有在选中异常时才需要在方法签名中删除异常。因为NullPointerException不是选中的异常