当我输入无int
时,我得到了输出。但是,CustomException
效果很好,但我不知道为什么InputMismatchException
不起作用。顺便说一句,s
是Scanner
的实例。
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at runMe.main(runMe.java:43)
int a=0;
try {
a = s.nextInt();
if (a < 1 || a > 9) {
throw new CustomException();
}
} catch(CustomException cusE2) {
System.out.println("Invalid input\n");
} catch(InputMismatchException inputE2) {
System.out.println("Invalid input type\n");
}