Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

a scjp question about Exception.

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q4
You are writing a set of classes related to cooking and have created your own exception hierarchy derived from java.lang.Exception as follows
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
Your base class, "BaseCook" has a method declared as follows
int rateFlavor(Ingredient[] list) throws BadTasteException
A class, "TexMexCook", derived from BaseCook has a method which overrides BaseCook.rateFlavor(). Which of the following are legal declarations of the overriding method?
a. int rateFlavor(Ingredient[] list) throws BadTasteException
b. int rateFlavor(Ingredient[] list) throws Exception
c. int rateFlavor(Ingredient[] list) throws BitterException
d. int rateFlavor(Ingredient[] list)
THE GIVEN ANS:acd.
I think the checked Exception which override method throws should be the same with supper class or the subclass Exception of the supper Exception ,concern the case the BitterException are not among the two case above ,so I think c is not right.
am I right?somebody correct me if I am wrong.
MY ANS:ad.

 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey I recognize that question. Don't you just hate the way this system throws away indenting characters.... The diagram is supposed to show that both BitterException and SourException descend from BadTasteException.
The rule is that overriding methods in a subclass can NOT throw a more general exception than the method in the parent class. A moment's reflection should tell you why. In this case it is b that is illegal.
Bill
------------------
author of:
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q4
You are writing a set of classes related to cooking and have created your own exception hierarchy derived from java.lang.Exception as follows
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
Your base class, "BaseCook" has a method declared as follows
int rateFlavor(Ingredient[] list) throws BadTasteException
A class, "TexMexCook", derived from BaseCook has a method which overrides BaseCook.rateFlavor(). Which of the following are legal declarations of the overriding method?
a. int rateFlavor(Ingredient[] list) throws BadTasteException
b. int rateFlavor(Ingredient[] list) throws Exception
c. int rateFlavor(Ingredient[] list) throws BitterException
d. int rateFlavor(Ingredient[] list)

I think the checked Exception which override method throws should be the same with supper class or the subclass Exception of the supper Exception ,concern the case the BitterException are not among the two case above ,so I think c is not right.
am I right?somebody correct me if I am wrong.
MY ANS:ad.
THE GIVEN ANS:acd.
 
Our first order of business must be this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic