ThrowsExam.java
package JavaStudy;
import java.util.Scanner;
//The process of dividing an integer by the entered value
//Exception handling when dividing by 0
public class ThrowsExam {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = 6873;
int retry = 0 ;
System.out.println("Start the program");
do {
System.out.print("Please enter a number.");
try{
int b = scan.nextInt();
double c = divide(a,b);
System.out.println(c);
}catch(ArithmeticException e) {
e.printStackTrace(); //Display error route / contents
System.out.println("The input value must be greater than 0.");
}
do { //Repeat when the retry value is other than 0 or 1.
System.out.print("Retry? [1 = yes / 0 = no]");
retry = scan.nextInt();
if(retry>1)
System.out.println("An invalid number has been entered.");
}while(!(retry <= 1 && retry >= 0));
}while(retry == 1);
System.out.println("Exit the program");
}
public static double divide(int a, int b) throws ArithmeticException {
return a / b;
}
}
Recommended Posts