public KalaGame(KeyBoardPlayer player1,KeyBoardPlayer player2)
 {   //super(0);
 int key=0;
 try
 {

     do{
     System.out.println("Enter the number of stones to play with: ");

      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        key = Integer.parseInt(br.readLine());


    if(key<0 || key>10)
  throw new InvalidStartingStonesException(key);
  }
 while(key<0 || key>10);
    player1=new KeyBoardPlayer();
   player2 = new KeyBoardPlayer();
   this.player1=player1;
   this.player2=player2;
   state=new KalaGameState(key);



 }


    catch(IOException  e)
    {
       System.out.println(e);
      }
     }


当我输入无效数量的宝石时,出现此错误

线程“主”中的异常InvalidStartingStonesException:起始石头的数量必须大于0且小于或等于10(尝试22)

为什么我定义的throw不处理异常

KalaGame.<init>(KalaGame.java:27) at PlayKala.main(PlayKala.java:10)

最佳答案

您只处理IOException,而不处理正在抛出的异常,即InvalidStartingStonesException

关于java - 我在主线程中遇到异常…即使在处理异常时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2614930/

10-14 11:44
查看更多