• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Question  RSS feed

 
Tony Gallagher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need some help with this practise program, the compiler will not allow me to create an IceCreamCone Object, the error is that the exception is unhandled. Any pointers would be appreciated, I just cant get it to work.

Thanks


Create an IceCreamConeException class whose constructor receives a String that consists of an ice cream cone’s flavour and number of scoops.
Create an IceCreamCone class with two fields, iceCreamFlavour and scoops.
The IceCreamCone constructor calls two data entry methods, getFlavour() and getScoops(). The getScoops() method throws an IceCreamConeException when the scoops exceeds three.
Write a test program that establishes several IceCreamCone objects and handles the Exception









 
Tony Gallagher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've changed my code around and managed to catch the exception but I'm not sure it answers the question correctly!

Any comments appreciated.

Thanks







 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony ew wrote:I'm not sure it answers the question correctly!

You're right - it doesn't.

Create an IceCreamConeException class whose constructor receives a String that consists of an ice cream cone’s flavour and number of scoops.
What parameters does your constructor have ?

Create an IceCreamCone class with two fields, iceCreamFlavour and scoops.
What are your fields called ?

The IceCreamCone constructor calls two data entry methods, getFlavour() and getScoops().
I could be wrong, but the way I read that requirement is that your constructor calls those two methods which each ask for information from the user. I don't think your IceCreamCone constructor should have any parameters.

The getScoops() method throws an IceCreamConeException when the scoops exceeds three.
This bit looks right.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch

Since the Exception class already has a field called message, you could use its constructor, which you can call with the super keyword.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony ew wrote: . . . The getScoops() method throws an IceCreamConeException when the scoops exceeds three. . . .
What a strange requirement. I would have thought you want to throw the Exception from the constructor when the number of scoops exceeds 3. You also have the opportunity to make the IceCream class immutable. I think that would work, but am not certain.
 
Brian Burress
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Tony ew wrote: . . . The getScoops() method throws an IceCreamConeException when the scoops exceeds three. . . .
What a strange requirement. I would have thought you want to throw the Exception from the constructor when the number of scoops exceeds 3. You also have the opportunity to make the IceCream class immutable. I think that would work, but am not certain.


I would have to agree - while I won't say "never", as a general statement data validation should occur when set on the object not when retrieved. In this case, ensure validation occurs in the constructor and the set statement otherwise you are left with an object with a state that is partially valid (i.e. the object exists and getFlavour will work but will possibly yield an exception on a getScoops).
 
qin weiping
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in think the problem is that in the class of the testIceCream you should throws the exception
-.jpg
[Thumbnail for -.jpg]
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Qin Weiping.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!