[問題] 例外處理機制 RuntimeException
在準備證照考試的時候看到一題,看不太懂
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
11/09 20:41, 1F
→
11/09 20:48, , 2F
11/09 20:48, 2F
→
11/10 12:44, , 3F
11/10 12:44, 3F