I have a code below. The question is what happens if the try/catch block is missing?
I have a code below. The question is what happens if the try/catch block is missing?
Also, could you explain the role of the variable stop?
ExceptionPropagation.java:
public class ExceptionPropagation {
public static void main(String[] args) {
ExceptionTest ext = new ExceptionTest();
ext.test();
}
}
ExceptionTest.java:
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionTest {
public void test(){
double num1;
double num2;
double sum;
boolean stop = false;
do{
try{
Scanner scan = new Scanner(System.in);
System.out.println(“Purpose: The program will add two numbers that you enter”);
System.out.print(“Enter the first number: “);
num1 = scan.nextDouble();
System.out.print(“Enter the second number: “);
num2 = scan.nextDouble();
sum = num1 + num2;
System.out.println(“The sum of ” + num1 + ” and ” + num2 + ” equals ” + sum + “.\n”);
stop = true;
}catch(InputMismatchException e){
System.out.println(“\nMake sure you enter a number!\nTry again!\n”);
}
}
while (!stop);