I am facing java.util.InputMismatchException;
I catch InputMismatchException but I don't understand why it is going into infinite loop after taking first wrong input and the output goes on like this:
enter two integers
exception caught
this goes on repeating
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int flag = 0;
while (flag != 1) {
try {
System.out.println("enter two integers");
int a = sc.nextInt();
int b = sc.nextInt();
int result = a + b;
flag = 1;
System.out.println("ans is" + result);
} catch (NumberFormatException e) {
System.out.println("exception caught");
} catch (InputMismatchException e) {
System.out.println("exception caught");
}
}
}