Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么时候在方法签名中使用关键字抛出?_Java_Exception - Fatal编程技术网

Java 什么时候在方法签名中使用关键字抛出?

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("

我正在阅读Helbert Schildt的Java Complete Reference,其中他使用异常展示了这个示例

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不是选中的异常