-1

好的,现在我已经更新了我的代码,它看起来像这样:

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."); 
}
}

这也在我的代码中:

  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"));
  }

但是现在我收到的错误是“错误:未报告的异常 BadShapeData;必须被捕获或声明为抛出”,并且在我使用 getDouble 方法的地方突出显示

4

6 回答 6

2

我会将您 finally 内部的 return 语句删除到外部。我对此不是 100% 的,但我认为它可能会吞下例外。

于 2012-10-25T18:59:23.447 回答
1

你传递了什么价值?请注意,如果您传递 a float、 an int、 along等。它将正确解析为 a double,因为所有这些类型都与 a 赋值兼容double。如果您想查看抛出的异常,则完全传递不同的类型,例如 string "xyz"

请注意 achar 一个数字,因此可以将其分配给double变量。例如,这一行不会导致编译或执行错误;这是完全有效的,尽管可能会令人困惑:

double c = 'x';

更新:

尝试像这样更改您的代码:

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

当然,你必须添加throws BadShapeData到方法声明中,像这样:

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

您还必须注意,从此时起,调用该getDouble()方法的代码中的所有部分都必须处理异常——通过捕获它或让它通过。正如您现在应该知道的,这就是 Java 中异常的工作方式。

于 2012-10-25T18:55:54.333 回答
1

将所有调用getDouble放在一个完整或单独的 try/catch 块中:

  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
   }
  }

或者已经makeShape抛出异常,然后在 void main() 中使用 try/catch 块:

  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"));
  }

您选择实施哪种方法取决于您的需求。如果 void main 不需要知道 BadShapeData 异常,请先使用。如果 void main 应该知道并对此采取措施,请选择第二个。

于 2012-10-25T19:40:44.440 回答
0

试试这个,然后:

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);
  }
}
于 2012-10-25T19:08:08.513 回答
0

删除 finally 块中的 return 语句。你只是吞下你的例外。而不是这个 put return finally 。

参考相关问题:异常被finally吞噬

更新: 抓住你的BadShapeData,因为它是一个检查的异常。另一种我更喜欢RuntimeException用作基类的方式。它更灵活,但不太安全。

于 2012-10-25T19:08:38.040 回答
0

您遇到的问题是您将异常隐藏在 finally 块中;

finaly 块在您抛出异常后执行。这意味着它永远不会消失,因为最终你会回来

您应该避免在 finall 块中使用 return 关键字。

于 2012-10-25T19:09:02.647 回答