Java 處理異常的問題

Given:
2. class MyException extends Exception { }
3. class Tire {
4. void doStuff() { }
5. }
6. public class Retread extends Tire {
7. public static void main(String[] args) {
8. new Retread().doStuff();
9. }
10. // insert code here
11. System.out.println(7/0);
12. }
13. }

I. void doStuff() {
II. void doStuff() throws MyException {
III. void doStuff() throws RuntimeException {
IV. void doStuff() throws ArithmeticException {
When fragments I – IV are added, independently, at line 10, which are true? (Choose all that apply.)
A. None will compile
B. They will all compile
C. Some, but not all, will compile
D. All of those that compile will throw an exception at runtime
E. None of those that compile will throw an exception at runtime
F. Only some of those that compile will throw an exception at runtime
答案:
C and D are correct. An overriding method cannot throw checked exceptions that are
broader than those thrown by the overridden method. However an overriding method can
throw RuntimeExceptions not thrown by the overridden method.
A, B, E, and F are incorrect based on the above. (Objective 2.4)

我覺得D這個答案很詭異,我自己選F,最主要是II的狀況會有編譯錯誤的問題,在Eclipse 3.5下會有錯誤?

很奇怪!

感謝你看到這裡,很快就可以離開了,但最好的獎勵行動就是按一下幫我分享或留言,感恩喔~

點我分享到Facebook

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。