Results 1 to 6 of 6
  1. #1
    maggie_2 is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default 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());
    
     }
    
    
    }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Exceptions need to be caught (or re-thrown) by methods calling the original method to throw the Exception.

  3. #3
    maggie_2 is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default 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());
      }
    
    
    
      
    
     
     
    
    
    }

  4. #4
    jeffj60 is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default 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.

  5. #5
    jeffj60 is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default

    Sorry, you need to use throw (new IceCreamConeException(String.valueOf(scoops)));
    for your code to compile. (not throws)

  6. #6
    angelicsign is offline Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    0

    Default

    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

  1. Runtime Exceptions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-12-2007, 11:31 AM
  2. how to handle exceptions
    By paty in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 05:17 AM
  3. Question on Exceptions
    By yelllow4u in forum New To Java
    Replies: 6
    Last Post: 07-27-2007, 02:41 PM
  4. Project of Exceptions
    By Albert in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 04:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •