Try-catch无法正常工作-InputMismatchException

时间:2018-05-07 16:40:38

标签: java oop exception-handling inputmismatchexception

System.out.println("Enter floor: ");
int f=x.nextInt();
p.setFloor(f);

此代码与对象

相关联
public void setFloor(int floor){
    try{
        this.floor = floor ;
    }catch (InputMismatchException e){
        System.out.println("Enter only digits");
    }
}

输出

Enter floor:
hesdd
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)

为什么捕获不起作用?

1 个答案:

答案 0 :(得分:4)

在致电Scanner.nextInt时,

const client = new AWSAppSyncClient({ url: AppSync.graphqlEndpoint, region: AppSync.region, auth: { type: AUTH_TYPE.AWS_IAM, credentials: () => Auth.currentCredentials() }, complexObjectsCredentials: () => Auth.currentCredentials(), }); 被抛出,你立即抓住它:

InputMismatchException