In the below code trying to take input from the user inside of a try-catch in a do-while loop. But it goes forever without taking the input again.
import java.util.Scanner;
public class Exceptions {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num;
boolean flag = true;
do {
try {
System.out.println("Enter a number: ");
num = scan.nextInt();
flag = false;
// Display the result
System.out.println("You entered the number: " + num);
}catch(Throwable e){
System.out.println("Wrong input! please try again!");
}
}while (flag);
}
}