无法在java中捕获InputMismatchException

时间:2015-11-07 00:11:25

标签: java exception exception-handling

当我输入无int时,我得到了输出。但是,CustomException效果很好,但我不知道为什么InputMismatchException不起作用。顺便说一句,sScanner的实例。

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");
}

0 个答案:

没有答案