Try & Catch in While Loop doesn't work!
I have been trying to get this simple try/catch block to repeat within a while loop using a boolean value. Every time there is no exception handled, the program works fine. When an exception is caught however, the catch block goes on to continuously repeat until the code is forced to stop. Here is the code:
Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
boolean loop = true;
while(loop == true){
try{
System.out.println("Enter a number:");
int usrInt = input.nextInt();
System.out.println("You wrote the number: " + usrInt);
loop = false;
}
catch(InputMismatchException e){
System.out.println("ERROR 01: Required type 'int' at line 12");
}
}
}
}
I want the code to continue looping after an exception is caught. I tried using "continue;" at the end of the catch block. It didn't work, and I found nothing on google that even slightly helped. Thanks for any help!