Why does this code keep on looping if there's an InputMismatchException?
When you input a zero, the catch
works, but if you enter a string, the loop becomes infinite.
import java.util.*;
public class TryCatch
{
public static void main(String args[])
{
int age, age2, sum;
boolean repeat=true;
Scanner input = new Scanner(System.in);
do
{
try
{
System.out.println("Enter age: ");
age = input.nextInt();
System.out.print("Enter age2: ");
age2 = input.nextInt();
sum = age / age2;
System.out.print(sum);
repeat=false;
}
catch (Exception e)
{
System.out.println("Your error is "+e+"\n Try again");
}
}
while(repeat == true);
}
}