This is a homework assignment where we have to debug and handle exceptions. I setup a try-catch to handle any input that isn't a double and the exception that comes up is caught, but it still prints the error. Here is my code:
import java.util.Scanner;
import java.util.InputMismatchException;
public class Paint1 {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double wallHeight = 0.0;
double wallWidth = 0.0;
double wallArea = 0.0;
double gallonsPaintNeeded = 0.0;
final double squareFeetPerGallons = 350.0;
do {
System.out.println("Enter wall height (feet): ");
try {
wallHeight = scnr.nextDouble();
throw new InputMismatchException();
} catch (InputMismatchException exc) {
System.out.println("Invalid input. Please try again.");
wallHeight = scnr.nextDouble();
}
} while (wallHeight <= 0);
...
...
}
}
and this is the output. Note: I used the number thirty spelled out to test the exception for the sake of the assignment.
Enter wall height (feet):
thirty
Invalid input. Please try again.
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at Paint1.main(Paint1.java:24)
What am I missing or what do I have that I'm not supposed to?