After catching an exception, how do I continue the execution of a Java program?
I made a program that asks a user to enter a number and it will return that number divided by a random number generated. However, if the user enters a letter like 'a', an exception will be caught.
How do I make the program continue its execution instead of terminating after it catches the exception?
do{ //Begin of loop
try{ //Try this code
System.out.println("Enter a number");
double i = read.nextDouble(); //Reads user input
double rn = r.nextInt(10); //Generates random number rn
System.out.println(i + " divided by random number " + rn + " is " + (i/rn));
}catch(InputMismatchException type_error){ //Catches error if there is a type mismatch
//Example error: if user enters a letter instead of a double
System.out.println("Error. You cannot divide a letter by a number!");
break; //break stops the execution of the program
}
//using a continue statement here does not work
}while(true); //Loop forever