• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic