Exception handling practice (ArithmeticException)

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

Exception handling practice (ArithmeticException)
[Java] Practice of exception handling [Exception]
Exception handling
Exception handling Exception
Java exception handling?
About exception handling
ruby exception handling
Ruby exception handling
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
About Ruby exception handling
[Ruby] Exception handling basics
Spring Boot exception handling
Classes that require exception handling
Java's first exception handling (memories)
[Java] About try-catch exception handling
[Ruby] Exception handling in functions
Java exception handling usage rules
Exception handling techniques in Java
[In-house study session] Java exception handling (2017/04/26)
exception
[Rails] How to write exception handling?
Exception handling with a fluid interface
Step-by-step understanding of Java exception handling
[For Java beginners] About exception handling
[Ruby] Exception handling, give back if given
Java (exception handling, threading, collection, file IO)
try-catch-finally exception handling How to use java