作者LaPass (LaPass)
看板java
標題[問題] 例外處理機制 RuntimeException
時間Wed Nov 9 20:23:29 2011
在準備證照考試的時候看到一題,看不太懂
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
→ sbrhsieh:checked/unchecked exception 有別。 11/09 20:41
→ LaPass:謝謝關鍵字,我去找找看 11/09 20:48
→ Chikei:JLS 3ed 8.4.6.倒數第二段,compile只檢查checked excp. 11/10 12:44