Results 1 to 6 of 6
Thread: Need Help With Exceptions
- 11-18-2008, 11:03 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 9
- Rep Power
- 0
Need Help With Exceptions
Please help,
I am working on this project for school, what I am trying to do is create an exception class whose constructor receives a String that consists of an ice cream cone's flavor and a integer representing the number of scoops in the IceCreamCone. Also, I am trying to create an IceCreamCone class with two fields-flavor and scoops. I need the IceCreamCone constructor to call data entry methods - setFlavor() and setScoops(). I need the setScoops() method to throw an IceCreamConeException when the scoops exceed 3. Any help would be greatly appreciated.
Java Code:public class IceCreamConeException extends Exception { public IceCreamConeException() { flav = iceCreamFlavor; scoops = nubmerOfScoops; } } import javax.swing.*; public class IceCreamCone { private String flavor; private int scoops; } public IceCreamCone() { setFlavor(); setScoops(); } public void setScoops() throws IceCeamConeException { if(scoops > 3;) throw(new IceCreamConeException()); } }
- 11-18-2008, 11:52 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Exceptions need to be caught (or re-thrown) by methods calling the original method to throw the Exception.
- 11-20-2008, 06:09 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 9
- Rep Power
- 0
Need Help with exception handling
I made some modifications to my original files, I can now compile the IceCreamConeException file, however I am still having trouble with the IceCreamCone file. When I try to compile I get only 1 error, the system complains that in line 29: " throws (new IceCreamConeException()); " is an illegal start of expression. Any suggestions would be greatly appreciated.
Java Code:public class IceCreamConeException extends Exception { public IceCreamConeException(String scoops) { super(scoops); } } import javax.swing.*; public class IceCreamCone { String flavor,inputString; int scoops; public IceCreamCone() throws IceCreamConeException { setFlavor(); setScoops(); } void setScoops() throws IceCreamConeException { flavor = JOptionPane.showInputDialog(null, " What flavor would you like? " ); inputString = JOptionPane.showInputDialog(null, " How many scoops would you like in your cone? "); scoops = Integer.parseInt(inputString); if(scoops > 3) throws (new IceCreamConeException()); } }
- 12-07-2008, 09:02 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 2
- Rep Power
- 0
Ice Cream Cone Exception throwing
I am not sure if you've discovered the answer yet.
Instead of throws (new IceCreamConeException());
use throw (new IceCreamConeException());
Throws is used on a method declaration.
Also, your constructor requires a parameter.
You will receive another error once you fix this unless
you pass a parameter or remove the parameter from
your Exception Constructor.
ie. you need to use throws (new IceCreamConeException(String.valueOf(scoops)));
for your code to compile.
- 12-15-2008, 06:37 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 2
- Rep Power
- 0
Sorry, you need to use throw (new IceCreamConeException(String.valueOf(scoops)));
for your code to compile. (not throws)
- 12-15-2008, 07:12 PM #6
Member
- Join Date
- Dec 2008
- Posts
- 25
- Rep Power
- 0
i use try and catch to complete your coding, hope this will help u up.
Java Code:void setScoops() { try{ flavor = JOptionPane.showInputDialog(null, " What flavor would you like? " ); inputString = JOptionPane.showInputDialog(null, " How many scoops would you like in your cone? "); scoops = Integer.parseInt(inputString); if(scoops > 3) throw new IceCreamConeException("Cannot more than 3 scoops"); } catch(IceCreamConeException es) { System.out.println(es.getMessage()); } }
Similar Threads
-
Runtime Exceptions
By Java Tip in forum Java TipReplies: 0Last Post: 11-12-2007, 10:31 AM -
how to handle exceptions
By paty in forum Advanced JavaReplies: 2Last Post: 08-05-2007, 04:17 AM -
Question on Exceptions
By yelllow4u in forum New To JavaReplies: 6Last Post: 07-27-2007, 01:41 PM -
Project of Exceptions
By Albert in forum Advanced JavaReplies: 1Last Post: 07-06-2007, 03:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks