Java 中 throw 和 throws 關鍵字有什麼區別?


throw 關鍵字用於顯式引發異常。

示例

public class Test {
   public static void main(String[] args) {
      throw new NullPointerException();
   }
}

執行緒“main”java.lang.NullPointerException 中的異常 at a6.dateAndTime.Test.main(Test.java:5)

Java 中的 throws 關鍵字用於推遲處理受檢異常。

public class Test {
   public static void main(String[] args)throws NullPointerException {
      throw new NullPointerException();
   }
}

更新時間: 20-2-2020

333 次瀏覽

開啟你的 職業生涯

透過完成課程獲取認證

開始
廣告
© . All rights reserved.