-2

I have the following code

//Ask for weight & pass user input into the object
        System.out.printf("Enter weight: ");
        //Check make sure input is a double 
        weight = input.nextDouble();
        weight =  checkDouble(weight);
        System.out.println(weight);
        System.exit(0); 

The Method checkDouble is

Double userInput;
public static Double checkDouble(Double userInput){ 
double weights = userInput;
 try{
    }catch(InputMismatchException e){
        System.out.println("You have entered a non numeric field value");
    }
    finally {
        System.out.println("Finally!!! ;) ");
    }
    return weights;
}

When I enter a letter instead of a number i receive the following error

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at HealthProfileTest.main(HealthProfileTest.java:42)

Why won't the wrong data type input hit the System.out.println() line in the catch block?

Kyl
  • 45
  • 2
  • 10

4 Answers4

3

As you see in stacktrace it is not thrown by your method but by nextDouble() call:

at java.util.Scanner.nextDouble(Scanner.java:2456)

And you call it here:

weight = input.nextDouble();

So you should cover this part by try catch:

try{

weight = input.nextDouble();
    }catch(InputMismatchException e){
        System.out.println("You have entered a non numeric field value");
    }
    finally {
    System.out.println("Finally!!! ;) ");
}
HRgiger
  • 2,750
  • 26
  • 37
1

The InputMismatchException is raised by the line weight = input.nextDouble();, which is not caught inside a try block, so the exception propagates out of your main method and crashes the program.

Your checkDouble() method doesn't actually check anything, it simply expects an empty try block to raise an InputMismatchException (which is impossible).

You need to call nextDouble() within a try block in order to catch the exception. For example:

try {
  double weight = input.nextDouble();
  System.out.println(weight);
} catch (InputMismatchException e) {
  System.out.println("Invalid number");
}

You may prefer to use Scanner.next() to read a string from the user, and then determine whether it's a valid double. This has the advantage of giving you the user's original input even if it's not valid.

String weightText = input.next();
try {
  double weight = Double.valueOf(weightText);
  System.out.println(weight);
} catch (NumberFormatException e) {
  System.out.println(weightText + " is not a valid double.");
}
dimo414
  • 47,227
  • 18
  • 148
  • 244
0

First of all you put nothing in try block. Second if you want to put any piece of code that raise exception and you want handle it you should put it in try-catch block.

Double userInput;
public static Double checkDouble(Double userInput){ 

try{
    double weights = userInput;
}catch(InputMismatchException e){
    System.out.println("You have entered a non numeric field value");
}
finally {
    System.out.println("Finally!!! ;) ");
}
return weights;
}
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27
0
Double weight = checkDouble(input);

public static Double checkDouble(Scanner userInput){ 
 Double weights = null;
 try{
      weights = userInput.nextDouble();
    }catch(InputMismatchException e){
        System.out.println("You have entered a non numeric field value");
    }
    finally {
        System.out.println("Finally!!! ;) ");
    }
    return weights;
}
Roman C
  • 49,761
  • 33
  • 66
  • 176