我正在用java编写代码,我遇到了这个错误:
try {
//can't use the memberlist??
System.out.println("Login successful! Welcome back!");
member.memberProfile.setFirstName("Jane");
member.memberProfile.setLastName("Doe");
member.memberProfile.setAddress("123, Foster avenue, Chicago, US");
member.memberProfile.setPhone("7735249286");
member.memberProfile.setInterests("music");
System.out.println("Do you wish to change your user information? Please type 'yes' or 'no': ");
String choice= input.nextLine();
if(choice.equals("yes")){
member.memberProfile.Update_Details();
}
System.out.println("Your user information is: ");
System.out.print(member.memberProfile.toString());
} catch(UserNotFoundException | WrongAnswerException e ){
System.out.println(e.getMessage());
tries ++;
}
我在第catch(UserNotFoundException | WrongAnswerException e )
行遇到问题
它说我需要移除UserNotFoundException
或WrongAnswerException
当我这样做时,它说我必须删除整个catch块。我能做什么?
答案 0 :(得分:2)
如果这两个都是已检查的异常(即它们不是RuntimeException
的子类),那么如果try
块中没有任何代码抛出这些异常,编译器将会抱怨。它不会让您捕获静态分析显示永远不会抛出的异常。
这与使用多捕获|
语法无关。
答案 1 :(得分:-3)
你做了一个合乎逻辑的OR
打破捕获区块
}catch(UserNotFoundException userNotFound ){
System.out.println(UserNotFound.getMessage());
tries ++;
}
}catch(WrongAnswerException wrongAnswer ){
System.out.println(wrongAnswer.getMessage());
tries ++;
}