• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

overriding a method... non checked exceptions?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot for your kind and fast answer
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic