Execution terminates even when exception is caught

Sameer Siddiqui :

I was running a simple Calculator app to learn Java's exception handling. I've set two exceptions to be handled: InputMismatchException and ArithmeticException for division by zero.

ArithmeticException gets handled and the do-while loop continues. But after catching the InputMismatchException, the execution terminates rather than continuing the loop.

Code:

do {
  result = 0;
  System.out.println("Enter the expression to calculate: (eg: 4 + 5) \n");
  try {
    num1 = input.nextInt();
    op = input.next().charAt(0);
    num2 = input.nextInt();
    result = a.Calc(num1, op, num2);             //Calc function to calculate
    System.out.println("= " + result);
  } catch (InputMismatchException e) {
     System.err.println("Please space out the expression");
  } catch (ArithmeticException e) {
     System.err.println("Cannot divide by zero");
  }
  System.out.println("Do you want to try again? (Y to retry)");
  OP = input.next().charAt(0);
} while (OP == 'Y' || OP == 'y');

Output:

Enter the expression to calculate: (eg: 4 + 5)
4 / 0
Cannot divide by zero                  //Exception handled
Do you want to try again? (Y to retry)
Y                                      //Do-while continues

Enter the question to calculate: (eg: 4 + 5)
4+5
Please space out the expression        //Exception handled
Do you want to try again? (Y to retry) //Rest of the code is executed
                                       //But the execution also terminates

Expected: Do-while to continue after InputMismatchException

Is this the correct way of doing this?

Andreas :

InputMismatchException is caused by the call to nextInt(), because the next token is 4+5.

The token is not consumed by the failed call.

Which means that OP = input.next().charAt(0) sets OP = '4', something that should be very apparent if you debugged the code. See What is a debugger and how can it help me diagnose problems? and How to debug small programs.

You need to discard the failed token(s), e.g. by calling nextLine() in the catch clause:

} catch (InputMismatchException e) {
    System.err.println("Please space out the expression");
    input.nextLine(); // Discard input(s)
} ...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325992&siteId=1