Java程序自定义异常会生成编译错误

时间:2016-04-11 03:11:38

标签: java

我正在尝试在Java中创建自定义异常,但是,当我创建自定义异常并尝试使用它时,我收到编译错误。我搜索了这个论坛并没有提供太多帮助,因为我尝试使用类似的代码,但它仍然无法解决问题!

以下是代码:

class CoogieException extends Exception {
  public int numCats;
  public String msg;

  public CoogieException() {

  }

  public CoogieException(String msg) {
       super();
       this.msg = msg;
  }

  public int getNumCats() {
       return numCats;
  }

}

和主要类 -

public class Lab12 {

 public int checkValue(int numCats) throws CoogieException {
     if (numCats != (int) numCats) {
         throw new CoogieException("Sorry, invalid entry");
     } else {
         return numCats;
     }
 }

 public static void main(String[] args) {

     CoogieException test = new CoogieException(); 
    Scanner in = new Scanner(System.in);
    System.out.println("Enter num of cats: ");
    int numCats = in.nextInt();
    try {
        lb = new Lab12();
        lb.checkValue(numCats);
    } catch (CoogieException co) {
        System.out.println("numCats is too many cats!!!!!");
    }

 }
}

... FYI

  Lab12.java:27: error: incompatible types: CoogieException cannot be     converted to Throwable
  public int checkValue(int numCats) throws CoogieException {
                                           ^
   Lab12.java:29: error: incompatible types: CoogieException cannot be converted to Throwable
         throw new CoogieException("Sorry, invalid entry");
         ^
  Lab12.java:119: error: incompatible types: CoogieException cannot be converted to Throwable
    } catch (CoogieException co) {

0 个答案:

没有答案