-1

Okay, Now that I've updated my code it looks like this:

public static double getDouble (String userShape, String parameter) throws BadShapeData
{

String missingValue = parameter, value = "", shape = userShape;
String s2 = "Enter a value for " + missingValue;

value = JOptionPane.showInputDialog(s2);

if (null == value || value.length() == 0) {
throw new BadShapeData("Error, nothing was entered. Must be double.");
}
try {
return Double.parseDouble(value);
}
catch (NumberFormatException e) {
throw new BadShapeData("Error entering " + value + ". Must be double."); 
}
}

And this is also in my code :

  public static void main(String args[]) {
  int choice;
  do {
       choice = menu();
       if(choice != 0) {
          System.out.println(makeShape(choice));
       }
   } while (choice != 0);
  }

  public static Shape3D makeShape(int choice) {
    if(choice == 1) 
      return new Cone(getDouble("Cone", "Radius"), getDouble("Cone", "Height"));
   else if(choice == 2) 
      return new Cylinder(getDouble("Cylinder", "Radius"), getDouble("Cylinder", "Height"));
   else if(choice == 3) 
      return new Sphere(getDouble("Sphere", "Radius"));
   else if(choice == 4) 
      return new Box(getDouble("Box", "Length"), getDouble("Box", "Width"), getDouble("Box", "Height"));
   else if(choice == 5) return new Pyramid(getDouble("Pyramid", "Base"), getDouble("Pyramid", "Height"));
   else return new Cube(getDouble("Cube", "Size"));
  }

However now the error I'm getting says "Error: unreported exception BadShapeData; must be caught or declared to be thrown" and is being highlighted where I use the getDouble method

David Cain
  • 16,484
  • 14
  • 65
  • 75
jrt18
  • 23
  • 6
  • Having a return statement in the finally block is a common mistake. Put the return statement **after** the finally block (and remove empty finally block). This way if there's an exception, it will be thrown. There won't be a return statement _inside_ the finally block preventing the throw. – ADTC Oct 25 '12 at 19:11
  • 1
    This is a cool mistake by the way – Adriaan Koster Oct 25 '12 at 19:15
  • I have summed up some general advices about working with exceptions in Java: [Catching pratice](http://codeisland.org/2012/catching-practice/) – Lukas Knuth Oct 25 '12 at 20:16

6 Answers6

2

I would remove the return statement that is inside your finally, to outside. I'm not 100% on this, but I think it might be swallowing the exception.

Darren
  • 722
  • 4
  • 12
1

And what value are you passing? notice that if you pass a float, an int, a long, etc. It will correctly parse as a double, because all those types are assignment-compatible with a double. If you want to see the exception being thrown, then pass a different type altogether, for example the string "xyz".

Be aware that a char is a number, so it's possible to assign it to a double variable. For example, this line will not result in a compilation or execution error; it's perfectly valid, albeit potentially confusing:

double c = 'x';

UPDATE:

Try changing your code like this:

try {
    return Double.parseDouble(value);
} catch (NumberFormatException e) {
    throw new BadShapeData(value);
}

Of course, you must add throws BadShapeData to the method declaration, like this:

public static double getDouble (String userShape, String parameter) throws BadShapeData

Also you must be aware that from this point onwards, all the parts in the code that call the getDouble() method will have to handle the exception - by catching it or letting it pass through. This is how Exceptions work in Java, as you should know by now.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • I am passing a char as the value, and it still runs through the program without any exceptions being thrown – jrt18 Oct 25 '12 at 18:56
  • @user1775214 a char is a number under the hood. For instance, this will NOT cause an error: `double c = 'x';` – Óscar López Oct 25 '12 at 18:58
  • I just passed it a string for the value and it still didn't work :( – jrt18 Oct 25 '12 at 19:00
  • @user1775214 are you sure? pass along, say, `ijk` – Óscar López Oct 25 '12 at 19:02
  • @user1775214 check the link in Darren Naylor's comment in his answer. – Luiggi Mendoza Oct 25 '12 at 19:03
  • @ÓscarLópez, That assigment is valid. But Double.parseDouble("X") will throw the exception. – Damian Leszczyński - Vash Oct 25 '12 at 19:04
  • I took out the finally statement and now it's giving me an error when I try and compile. It says "unreported exception BadShapeData; must be caught or declared to be thrown" and I tried ijk Oscar, no luck – jrt18 Oct 25 '12 at 19:05
  • Okay, now I keep getting "Error: unreported exception BadShapeData; must be caught or declared to be thrown" when I try and compile it. But I get what you're saying with returning it in the try, because it's expecting a double. Now I just need to figure out this error – jrt18 Oct 25 '12 at 19:09
  • @user1775214 again, see the end of my updated question please – Óscar López Oct 25 '12 at 19:11
  • 1
    @user1775214 when you add `throws BadShapeData` to the method, anywhere you call the method must either throw it as well or use a try/catch block to handle the incoming BadShapeData exception. This is how Java functions, and that's why you get "must be caught or declared to be thrown" error. – ADTC Oct 25 '12 at 19:16
  • So how exactly would I fix that? I put the code I'm using for my program at the top @ADTC – jrt18 Oct 25 '12 at 19:18
  • @user1775214 read about exception handling in a Java tutorial, this is outside the scope of the question. Google for it, you need to learn that before continuing with your coding efforts. – Óscar López Oct 25 '12 at 19:21
  • @user1775214 see my answer. There are two ways. I have given code for both. – ADTC Oct 25 '12 at 19:41
1

Either place all calls to getDouble in a full or individual try/catch block(s):

  public static void main(String args[]) {
  int choice;
  do {
       choice = menu();
       if(choice != 0) {
          System.out.println(makeShape(choice));
       }
   } while (choice != 0);
  }

  public static Shape3D makeShape(int choice) {
   try {
    if(choice == 1) 
      return new Cone(getDouble("Cone", "Radius"), getDouble("Cone", "Height"));
   else if(choice == 2) 
      return new Cylinder(getDouble("Cylinder", "Radius"), getDouble("Cylinder", "Height"));
   else if(choice == 3) 
      return new Sphere(getDouble("Sphere", "Radius"));
   else if(choice == 4) 
      return new Box(getDouble("Box", "Length"), getDouble("Box", "Width"), getDouble("Box", "Height"));
   else if(choice == 5) return new Pyramid(getDouble("Pyramid", "Base"), getDouble("Pyramid", "Height"));
   else return new Cube(getDouble("Cube", "Size"));
   } catch (BadShapeData e) {
      System.out.println(e.getMessage());
      //do whatever with exception
   }
  }

OR have makeShape throw the exception then use a try/catch block in void main():

  public static void main(String args[]) {
  int choice;
  do {
       choice = menu();
       if(choice != 0) {
         try {
          System.out.println(makeShape(choice));
         } catch (BadShapeData e) {
            System.out.println(e.getMessage());
            //do whatever with exception
         }
       }
   } while (choice != 0);
  }

  public static Shape3D makeShape(int choice) throws BadShapeData {
    if(choice == 1) 
      return new Cone(getDouble("Cone", "Radius"), getDouble("Cone", "Height"));
   else if(choice == 2) 
      return new Cylinder(getDouble("Cylinder", "Radius"), getDouble("Cylinder", "Height"));
   else if(choice == 3) 
      return new Sphere(getDouble("Sphere", "Radius"));
   else if(choice == 4) 
      return new Box(getDouble("Box", "Length"), getDouble("Box", "Width"), getDouble("Box", "Height"));
   else if(choice == 5) return new Pyramid(getDouble("Pyramid", "Base"), getDouble("Pyramid", "Height"));
   else return new Cube(getDouble("Cube", "Size"));
  }

Which method you choose to implement depends on your needs. Go with first if void main need not know of the BadShapeData exception. Go with second if void main should know and do something about it.

ADTC
  • 8,999
  • 5
  • 68
  • 93
  • @user1775214 Why as there an attempt to blank out the answer? I don't think SO community allows that. – ADTC Oct 26 '12 at 02:44
0

Try this, then:

public static double getDouble (String userShape, String parameter) {
  String prompt = "Enter a value for " + parameter;
  String value = JOptionPane.showInputDialog(prompt);

  if (null == value || value.length() == 0) {
    throw new BadShapeData("Error, nothing was entered. Must be double.");
  }
  try {
    return Double.parseDouble(value);
  }
  catch (NumberFormatException e) {
    throw new BadShapeData("Error entering " + str + ". Must be double."); 
  }
}

public class BadShapeData extends RuntimeException {
  public BadShapeData(String message) {
    super(message);
  }
}
Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
  • I don't like it when people simply post a bunch of code without explaining the improvements/mistakes. You and I know that people tend to just copy every "Gimme the CodZ" solution right into their IDE without spending a second on thinking about it. – Lukas Knuth Oct 25 '12 at 19:29
  • And the OP is in for a real surprise when he discovers that a `RuntimeException` was _not_ what his teacher was expecting from him/her, or when his program crashes because of the `RuntimeException` being thrown unexpectedly. I suggested he learned how to handle exceptions in Java, but instead opted for the quick hack suggested here. – Óscar López Oct 25 '12 at 19:32
  • Why are you extending RuntimeException? @Dilum – ADTC Oct 25 '12 at 19:48
  • For your information @ÓscarLópez, I'm a girl. Second, I didnt even extend RunTimeException because I knew that wasn't what I needed to do. So maybe you shouldn't just assume that I was just taking people's code without thinking about it. And I obviously have been trying to figure it out online for over two hours now which is the only reason I came to this site in the first place. I've been googling for literally the past two hours, skipped my calculus class, and still couldn't figure it out. So chill. – jrt18 Oct 25 '12 at 20:05
  • @user1775214 FYI: you chose the wrong answer for your problem. Go and read a tutorial or ask your teacher. So learn. – Óscar López Oct 25 '12 at 20:08
0

Remove return statement in finally block. You just swallow your exception. Instead of this put return outside finally.

Refer related question: Exception is swallowed by finally

UPDATE: Catch your BadShapeData as it is a checked exception. Another way, which I prefer, to use RuntimeException as base class. It's more flexible, though less safe.

Community
  • 1
  • 1
mishadoff
  • 10,719
  • 2
  • 33
  • 55
0

The problem you have is that you hide the exception in finally block;

The finaly block is executed after you throw an exception. This mean it never go out as in finally you return

You should avoid to use return key word in finall block.