Ниже выложил код. Если в консоли умышленно ввести текст вместо число, то программа зацикливается и бесконечно обрабатывает исключение InputMismatchException
for (int i = 0; i < Count; i++) {
System.out.println("Enter the number");
try {
userNumber = in.nextInt();
if (!(userNumber > 0 && userNumber < 11))
throw new Exception("The number must be greater than 1 and less than 10. ");
}
catch (InputMismatchException err) {
System.out.println("Type a number, not a text");
i--;
continue;
}
catch (Exception err) {
System.out.println(err.getMessage());
i--;
continue;
}
Выводит что-то вроде этого:
Enter the number
asdasd
Type a number, not a text
Enter the number
Type a number, not a text
Enter the number
Type a number, not a text
Enter the number и т.д.