创建异常类并在代码中使用它

时间:2012-10-25 18:53:37

标签: java

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

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方法的地方

6 个答案:

答案 0 :(得分:2)

我会删除最终内部的返回语句到外部。我不是100%就此,但我认为它可能会吞下这个例外。

答案 1 :(得分:1)

你传递的是什么价值?请注意,如果您传递的是floatintlong等,那么正确解析为double,因为所有这些类型与double分配兼容。如果要查看抛出的异常,请完全传递其他类型,例如字符串"xyz"

请注意,char 是一个数字,因此可以将其分配给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()方法的所有部分都必须处理异常 - 通过捕获它或让它通过。这就是Exceptions在Java中的工作方式,正如您现在应该知道的那样。

答案 2 :(得分: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
   }
  }

OR 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异常,请先使用。如果无效的主要应该知道并做一些事情,请使用第二个。

答案 3 :(得分: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);
  }
}

答案 4 :(得分:0)

删除finally块中的return语句。你只是吞下你的例外。而不是把它最终放回到外面。

参考相关问题:Exception is swallowed by finally

<强>更新 抓住您的BadShapeData,因为它是已检查的例外。另一种方式,我更喜欢使用RuntimeException作为基类。它更灵活,但安全性更低。

答案 5 :(得分:0)

你遇到的问题是你在finally块中隐藏了异常;

在抛出异常后执行最终块。这意味着它永远不会像最后一样回归

你应该避免在finall块中使用返回关键字。