-1

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

xingbin
  • 27,410
  • 9
  • 53
  • 103
J. Doem
  • 619
  • 7
  • 21
  • 5
    A try catch block is not a loop – Lance Toth Feb 27 '18 at 13:11
  • By the way, you shouldn't be using exception handling as part of your implementation logic. It is to be used to handle exceptional scenarios outside your expectation. – user3437460 Feb 27 '18 at 13:20
  • Read the javadocs for `nextInt`. If the method call throws `InputMismatchException`, it does not skip the characters making up the token that it it couldn't parse as an integer. So if you call `nextInt` again you will attempt to parse the same token *again* as an integer ... and fail *again*. – Stephen C Feb 27 '18 at 13:27
  • @user3437460 - If the OP's goal is to learn how exceptions / handlers work, then this is just fine. And besides, it is hard to make a judgment that something is an "exceptional scenario" or not without a significant amount of context. More context than we have here. – Stephen C Feb 27 '18 at 13:30
  • 1
    J. Doem - also look at what your `printf` is actually doing. It is (trying to) read a number, and then inserting that numbet into the message which is asking to enter a number. That's not correct ... is it. – Stephen C Feb 27 '18 at 13:35

1 Answers1

3

try catch finally block works like this:

  1. Execute the code in try block, in this case, let the user enter something.
  2. If exceptions which specified in catch are thrown, then execute the code in catch block only once.
  3. Execute the code in finally block.

If you want to wait until the user input an int, you should use for or while loop:

Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int i;

while (true) {
    try {
        i = Integer.parseInt(in.nextLine());
        System.out.println("Your input is " + i);
        break;
    } catch (NumberFormatException exception) {
        System.out.println("Please enter a number:");
    }
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • Why use `Integer.parseInt(in.nextLine())` instead of `in.nextInt()`? – J. Doem Feb 28 '18 at 13:11
  • 1
    @J.Doem See this https://stackoverflow.com/questions/3572160/how-to-handle-infinite-loop-caused-by-invalid-input-inputmismatchexception-usi – xingbin Feb 28 '18 at 13:24