File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes trying to create exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trying to create exception" Watch "trying to create exception" New topic
Author

trying to create exception

David Stanley
Greenhorn

Joined: Dec 17, 2003
Posts: 1
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

Joined: Jul 03, 2001
Posts: 2120
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


SCJP2. Please Indent your code using UBB Code
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trying to create exception
 
Similar Threads
child classes
Need help with exception
Need help with project
Need help with code
Exception Question