我在主线程中遇到异常...即使在处理异常时 [英] I am getting exception in main thread...even when i am handling the exception

查看:92
本文介绍了我在主线程中遇到异常...即使在处理异常时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

when i enter an invalid number of stones i get this error

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

Exception in thread "main" InvalidStartingStonesException: The number of starting stones must be greater than 0 and less than or equal to 10 (attempted 22)

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

why isn't the exception handled by the throw i defined at

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

推荐答案

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

You are only handling an IOException but not the exception that is being thrown, i.e. an InvalidStartingStonesException.

这篇关于我在主线程中遇到异常...即使在处理异常时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆