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


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

示例

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

thread "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();
   }
}

更新於: 2020 年 2 月 20 日

332 次瀏覽

開啟你的 職業

完成課程後獲得認證

開始學習
廣告
© . All rights reserved.