Hi,
mir ist aufgefallen, dass ich die InputMismatchException für eine Verwendung importieren muss.
Die "standard" Exception muss ich aber nicht importieren.
Liegt es daran, dass die "standard" Exception in der Klasse "Object" bereits definiert ist, die InputMismatchException aber nicht?
mir ist aufgefallen, dass ich die InputMismatchException für eine Verwendung importieren muss.
Die "standard" Exception muss ich aber nicht importieren.
Liegt es daran, dass die "standard" Exception in der Klasse "Object" bereits definiert ist, die InputMismatchException aber nicht?
Code:
package Exceptions;
import java.util.Scanner;
import java.util.InputMismatchException; //muss ich nicht bei (Exception ex) importieren
public class MainClass {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
while (true) {
System.out.println("Zahl eingeben: ");
try {
double i = userInput.nextDouble();
System.out.println("Eingegeben wurde: " + i);
break;
} catch (InputMismatchException ex) { //oder allg:(Exception ex)
userInput.next();
System.out.println("Falsche Eingabe");
}
}
}
}
Zuletzt bearbeitet: