但是然后在以下程序中,当catch语句中重新引发异常且没有throws子句时,就没有错误了?
怎么样?

Class Throwdemo {
  static void demoproc(){
    try{
        throw new NullPoinerException ("demo");
    }catch(NullPointerException e) {
        System.out.println("Caught inside demoproc.");
        throw e;
    }
  }
  public static void main(String Args[]){
    try[
        demoproc();
    }catch(NullPointerException e) {
            System.out.println("Recaught : " + e);
    }
}
}


输出是

Caught inside demoproc.
Recaught : java.lang.NullPointerException: demo

最佳答案

您只需要throws子句即可检查异常。

10-08 03:25