2016-04-11 14 views
-1

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

は ''「私はカスタム例外を作成するときしかし、私はコンパイルエラーを取得する」 - あなたの質問との完全なコンパイラエラーを投稿することが賢明ではないでしょうか? –

+0

これは次のように注意してください: 'public CoogieException(String msg){super(); this.msg = msg; } 'は次のように変更する必要があります:' public CoogieException(String msg){super(msg);} } –

+0

コンパイルエラーが含まれています。 – ABY

答えて

0

は、おそらくあなたにはないどこかExceptionと呼ばれるクラスを持っているが、 java.lang.Exception(またはThrowableを実装)を拡張します。同じ問題がある別のクラスCoogieExceptionもあります。

+0

私はそれを得ました。私はjava.lang.Exceptionをインポートする必要がありました。 – ABY

関連する問題