[問題] 例外處理機制 RuntimeException

看板java作者 (LaPass)時間14年前 (2011/11/09 12:23), 編輯推噓0(003)
留言3則, 3人參與, 最新討論串1/1
在準備證照考試的時候看到一題,看不太懂 1 class X{ 2 public void foo(){} 3 } 4 5 public class test03 extends X{ 6 public static void main(String[] args){} 7 public void foo() throws RuntimeException{} 8 } 第七行如果改成throws Exception{}會編譯失敗 理由是 Override時,throws的類型,必需是原方程式 throws 的類別或是子類別 如果原方程式沒 throws 例外出來,那Override的方程式也不能 throws 例外出來 那為什麼第七行加了 throws RuntimeException 能編譯成功? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.41.109.226

11/09 20:41, , 1F
checked/unchecked exception 有別。
11/09 20:41, 1F

11/09 20:48, , 2F
謝謝關鍵字,我去找找看
11/09 20:48, 2F

11/10 12:44, , 3F
JLS 3ed 8.4.6.倒數第二段,compile只檢查checked excp.
11/10 12:44, 3F
文章代碼(AID): #1Ekd341f (java)