-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

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.

于 2012-10-25T18:59:23.447 に答える
1

そして、あなたはどのような値を渡していますか?floata 、 an int、 alongなどを渡すと、 aとして正しく解析されることに注意してください。doubleこれらの型はすべて a との代入互換性があるためdoubleです。例外がスローされていることを確認したい場合は、別の型 ( string など) を一緒に渡します"xyz"

char aは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 がそれを認識して何かを行う必要がある場合は、second を使用します。

于 2012-10-25T19:40:44.440 に答える
0

問題は、finally ブロックで例外を非表示にすることです。

finaly ブロックは、例外をスローした後に実行されます。これは、最終的に戻ってくるように、決して外出しないことを意味します

finall ブロッ​​クで return キーワードを使用することは避けてください。

于 2012-10-25T19:09:02.647 に答える
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 ステートメントを削除します。例外を飲み込むだけです。この代わりに、最後にリターンを外に置きます。

関連する質問を参照してください:例外は最後に飲み込まれます

更新:BadShapeDataチェックされた例外であるため、 キャッチしてください。私が好む、RuntimeException基本クラスとして使用する別の方法。安全性は劣りますが、より柔軟です。

于 2012-10-25T19:08:38.040 に答える