Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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

Exception!

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.
Can someone shed some light on this question. It's from Bill Brogden's Exam Cram which is a really good book. I just don't quite follow the wording of this particular question & I would appreciate your help.
Here is the question:
You are writing a set of classes related to cooking and have your own exception heirarchy derived from java.lang.Exception as follows:
Exception
+- BadTasteException
+- BitterException
+- SourException
BadTasteException is defined as an abstract class.
You have a method eatMe that may throw a BitterException or a SourException. Which of the following method declarations will be acceptable to the compiler?
A) public void eat me(Ingredient [] list)throws BadTasteException
B) public void eat me(Ingredient [] list)throws BitterException,SourException
C) public void eat me(Ingredient [] list)may throw BadTasteException
D) public void eat me(Ingredient [] list)

I answered B & D. The correct answer from the book is A & B.
Generally speaking I would say I'm quite reasonable with exceptions, but I am stumped with this one. I think the biggest problem is I don't fully understand what the question is asking, I mean no offense to Bill as this has been a really helpful book which I highly recommend.
Can someone explain this a little more please?
Thanks, Dave.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is this: Which of the four given method signatures are legal for a method which is named eatMe and throws BitterException and SourException?
Hope that clears it up
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My own vain attempt would be this:
The various eatMe () methods are in this BadTasteException, which may well throw BitterTasteException or SourException.
A is acceptable because it is more general but yet suitable to handle either exception.
B is acceptable as this handle the excact exceptions named in the question.
C is a sytax error
D Is not correct as it does not attempt to handle the errors that may be thrown.
It is just first 10 or so times I read this question I could invisage quite what the senario was. That is half my problem understanding exactly what I am being asked?
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The various eatMe () methods are in this BadTasteException, which may well throw BitterTasteException or SourException.


The method eatMe() is assumed to be in someother class, not in the exception class. Since, the method may throw BitterTasteException or SourException , you declare the method to throw these exceptions explicitly or throw any of its super classes, that is BadTasteException or Exception.
 
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one baffles me. First off, unless there has been a transcription error, none of these method declarations would compile because the method names are wrong. Assuming they should be eatMe, no one declares abstract exception classes.
 
Are you here to take over the surface world? Because this tiny ad will stop you!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic