It says that the only right answers are C and D, but B also compiles, right? It is never a problem to add a RuntimeException to the method throws clause. For example, I have the Daddy class, which has the methodX() that throws nothing. And class Son extends Daddy, which throws RuntimeException, this compiles fine!!! i really think that the ansswer of this question ios wrong, and you? [ adjusted spacing in code block ] [ May 26, 2002: Message edited by: Jessica Sant ]
I think you're right. It's never required to specify any non-checked exceptions in your throws clause, but specifying them is certainly allowed. I'd say B is a correct answer as well. (Note however, in a real program you usually won't specify any non-checked exceptions because that will just clutter up your code and doesn't add a lot of value.) [ May 26, 2002: Message edited by: Rob Ross ]
A non-checked Exception is not required to be thrown,I think sun provide this is to let the programmer to deal with the Runtime Exception in catch clause,and also not deal with it at all,it is up to you.
Hi Just to clarify again. Thus, the correct answer for the question is B,C, D, and E. B and E are correct because specifying non-checked exceptions in your throws clause is allowed although not required. Correct me if I'm wrong. Thanks a lot.
--------------------<p>Karen Leoh<br />Sun Certified Programmer for Java™ 2 Platform
In "Effective Java" Joshua Bloch says that you should specify only checked exceptions in the methods throws clause, and that you should specify the unchecked exceptions in the javadoc documentation for the method. Both are legal, but this is better style.
E is not correct -- although it declares that it throws the Unchecked exception (RuntimeException) -- it doesn't declare that it throws the checked exception (Base/SubException). And hence would through a compile time error.
subject: overriding a method... non checked exceptions?