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) {
は ''「私はカスタム例外を作成するときしかし、私はコンパイルエラーを取得する」 - あなたの質問との完全なコンパイラエラーを投稿することが賢明ではないでしょうか? –
これは次のように注意してください: 'public CoogieException(String msg){super(); this.msg = msg; } 'は次のように変更する必要があります:' public CoogieException(String msg){super(msg);} } –
コンパイルエラーが含まれています。 – ABY