我有一个读取整数作为输入的菜单,这里是菜单的方法:
public int menu(String _menuHeader,String[] _menuItems) throws InvalidInputException {
int choice = 0;
do {
try {
scanner.nextLine();
System.out.println(_menuHeader);
for (int i = 0; i < _menuItems.length; i++) {
System.out.println(" " + (i + 1) + " " + _menuItems[i]);
}
choice = scanner.nextInt();
if (choice <= 0 || choice > _menuItems.length) {
throw new InvalidInputException();
}
} catch (Exception e) {
System.out.println("Enter valid input");
validInput = false;
} catch (InvalidInputException e) {
System.out.println("Please enter a choice between 1 and" + _menuItems.length);
validInput = false;
}
} while (!validInput);
}
现在我想在输入超出允许选择范围时捕获异常,即输入 7 用于选择 1 和 2,
为此,我尝试使用 InvalidInputException,但这会导致编译错误为“找不到符号 InvalidInoutException”,尽管我已导入“import.java.Throwable/Exception;”