Tuesday, August 10, 2010

Exceptions: CC.java


Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 5, Question 7

3. class SubException extends Exception { }
4. class SubSubException extends SubException { }
5.
6. public class CC { void doStuff() throws SubException { } }
7.
8. class CC2 extends CC { void doStuff() throws SubSubException { } }
9.
10. class CC3 extends CC { void doStuff() throws Exception { } }
11.
12. class CC4 extends CC { void doStuff(int x) throws Exception { } }
13.
14. class CC5 extends CC { void doStuff() { } }






What is the result? (Choose all that apply.)
  1. Compilation succeeds
    Incorrect, see following choices.


  2. Compilation fails due to an error on line 8
    Incorrect, an overriding method can throw a narrower exception, hence this line will compile.


  3. Compilation fails due to an error on line 10
    Correct, this overriding method throws a broader exception, hence this line will not compile.


  4. Compilation fails due to an error on line 12
    Incorrect, this method is simply an overload, so the issue of throwing broader or narrower exception is mute, hence this line will compile.


  5. Compilation fails due to an error on line 14
    Incorrect, this line will compile.

No comments:

Post a Comment