I am learning about java try catch and using the following code
public static void main(String[] args) {
Scanner in = null;
int i = 0;
try {
in = new Scanner(System.in);
System.out.print("Enter a number: ");
i = in.nextInt();
} catch (InputMismatchException ex) {
System.out.printf("%nPlease enter a number: %d", in.nextInt());
} finally {
if (in != null) {
System.out.println();
System.out.println("Finally block !!!");
in.close();
}
}
}
Running those program and input a string return java with stack trace and exit (not asking for user to input correct number). If i remove in.nextInt() inside catch block, I do not see stack trace but not asking for user input too - exit immediately.
I can't figure it out what is wrong with my code