現在次のコードを理解しようとしているのですがなぜコンパイルエラーになるか理解できずにいます。
java
1class MyEx extends RuntimeException { } 2class A { 3 void foo() throws RuntimeException { 4 throw new RuntimeException(); 5 } 6} 7class B extends A { 8 void foo() throws MyEx { 9 throw new Exception(); 10 } 11} 12class Test { 13 public static void main(String[] args) { 14 try { 15 A obj = new B(); 16 obj.foo(); 17 } catch (Exception e) { 18 System.out.println(e); 19 } 20 } 21} 22
『throw new Exception();』でコンパイルエラーになります。
内容は「処理されない例外の型 Exception」です。
ただなぜエラーになるのかわかりません。
もし分かる方がいれば教えていただけると助かります。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/09 14:50