Ich versuche, eine benutzerdefinierte Ausnahme in Java zu erstellen, jedoch erhalte ich einen Kompilierfehler, wenn ich die benutzerdefinierte Ausnahme erstellen und versuchen, sie zu verwenden. Ich habe dieses Forum durchsucht und nicht viel Hilfe zur Verfügung gestellt, weil ich versuche, ähnlichen Code zu verwenden, und es immer noch nicht das Problem behebt!Java-Programm benutzerdefinierte Ausnahme erzeugt einen Kompilierfehler
Hier ist der Code:
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;
}
}
und die Hauptklasse -
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) {
'" jedoch bekomme ich einen Kompilierfehler, wenn ich die benutzerdefinierte Ausnahme "' - dann wäre es nicht klug, den kompletten Compilerfehler mit Ihrer Frage zu veröffentlichen? –
Beachten Sie Folgendes: 'public CoogieException (String msg) {super(); this.msg = Nachricht; } sollte geändert werden in: 'public CoogieException (String msg) {super (msg); } –
enthalten Kompilierungsfehler. – ABY