This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trying to create exception

 
David Stanley
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new and am having a hard time with this, can anyone tell me whats wrong here.
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class IceCreamCone
{
// String flavor = new String();
String flavor;
int scoops;

public void IceCreamCone()Exception, throws iceCreamException
{
flavor = getFlavor();
scoops = getScoops();
}
String getFlavor() throws Exception
{
System.out.println("Enter the desired flavor ");
char newChar;
String inputString = new String();
newChar = (char)System.in.read();
while(newChar >='a' && newChar<='z')
{
inputString = inputString + newChar;
newChar = (char)System.in.read();
}
System.in.read();
return(inputString);
}
int getScoops() throws Exception, iceCreamException
{
int Nos;
String inString = new String();
char inChar;
System.out.println("Enter number of scoops (3 is Max)");
inChar = (char)System.in.read();
while(inChar >= '0' && inChar <='9')
{
inString = inString + inChar;
inChar = (char)System.in.read();
}
System.in.read();
Nos = Integer.parseInt(inString);
if (Nos > 3)
throw(new iceCreamException(flavor + " " + Nos));
return(Nos);
}
}
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch David.
Notice that IceCreamCone is not a constructor because has a return type (void). It is a method. Anyway being a bit more explicit about the question would be helpful.
This post does not fit in the Swing forum. I am asking for them to move it to the beginner one. Please do not post anything else on this post untill it is moved.
Thanks for posting to this site
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void IceCreamCone()Exception, throws iceCreamException
Not sure what you are trying to say here, but the compiler will flag this line. Perhaps you meant:

That will compile (assuming that you have defined class iceCreamException somewhere). However, it still isn't Java-like. First, as Jose already pointed out, it looks like a constructor, but it's not, because it has a return type, and it's probably not what you intended. Second, it's a good practice not to throw an amorphous Exception from a method, but rather a subset of specialized exceptions, -- this will allow for a meaningful processing of the exceptions when they are caught somewhere above the call stack.
[ February 08, 2004: Message edited by: Eugene Kononov ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic