• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding a method... non checked exceptions?

 
Paulo Silveira
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from this mock:
http://valiveru.tripod.com/java/jvaltest.html


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 ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Paulo Silveira
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for your kind and fast answer
 
Steven Sun
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karen Leoh
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E option is not correct as it does not specify the checked exception(Base or its superclass Exception) in the throws clause.
 
Steve Chernyak
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
moving this to mock exam errata now that its been determined the answer is incorrect. Please continue the dicsussion there.
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is the conclusion? Is E is correct or not? I don't think E is correct.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic