Answer the question
In order to leave comments, you need to log in
Does the program loop when handling the exception (InputMismatchException) thrown by Scanner?
Posted the code below. If you deliberately enter text instead of a number in the console, then the program loops and endlessly handles the InputMismatchException exception
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;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question