import java.util.Scanner;
class Division
{
public static void main(String[] args)
{
int a, b, result;
Scanner input = new Scanner(System.in);
System.out.println("Input two integers");
a = input.nextInt();
b = input.nextInt();
// try block
try
{
result = a / b;
System.out.println("Result = " + result);
}
// catch block
catch (ArithmeticException e)
{
System.out.println("Exception caught: Division by zero.");
}
}
}
Step-by-Step Explanation of the Program:
This program is an example of exception handling in Java, where the program handles the ArithmeticException while performing division of two integers taken as input from the user and handles the situation when it is divided by zero.
1. Importing Scanner Class:
Scanner input = new Scanner(System.in);
- Scanner: The Scanner class is used to read input from the user. Here, it is used to read two integers from the user.
- It will request the user to enter two integers for division.
2. Declaration of Variables:
int a, b, result;
- Three integer variables a, b and result are declared.
- The integers will be input by the user and stored in variables a and b.
- The result of the division operation will be stored in result.
3. Getting User Input:
System.out.println(“Input two integers”);
a = input.nextInt();
b = input.nextInt();
- The user is asked to enter two integers.
- The integers that are given are assigned to variables a and b.
4. Division Operation Inside try Block:
try {
result = a / b;
System.out.println(“Result = ” + result);
}
- Codes contained within a try block throw exceptions when it executes. Here the division a / b is made.
- If the user inputs b = 0, an ArithmeticException will be thrown because division by zero is not allowed.
- If the division is successful (i.e., b is not zero), the result of the division is stored in result and printed to the console.
5. Handling the Exception:
catch (ArithmeticException e) {
System.out.println(“Exception caught: Division by zero.”);
}
- If there is an exception thrown from the try block; that is, an ArithmeticException which occurs when there is division by zero, then the program catches it in the catch block.
- In the case of a division by zero, the program in the catch block will be invoked, which will print out “Exception caught: Division by zero.” to the console.
- The e object represents the exception thrown, but this program does not use that. You could, however, print the details of the exception using e.printStackTrace() if desired.
6. Program Flow:
- If you enter valid integers and do not attempt to divide by zero, the program will print the result of the division.
- The program will detect the division by zero exception when the user tries to do that and will print the error message.
Example Scenarios
1. Normal Input (Valid Division):
The user inputs the following values, namely a = 10 and b = 2.
Then, the program will compute the division of 10 divided by 2, and display its result in this way.
Result = 5
2. Division by Zero (Invalid Division):
User enters a = 10 and b = 0.
The program tries to divide by zero, and it raises an exception. The program catches the exception and prints:
Exception caught: Division by zero.
Examples of Outputs
1. Correct Division, where a = 10 and b = 2:
Enter two integers
10
2
Output= 5
2. Division by Zero, with the example set where a = 10, and b = 0 :
Input two integers
10
0
Error: attempted division by zero.
Summary:
- Programmatically this application reads in two integers provided by the user then tries to divide the first number by the second.
- It uses a try-catch block to handle potential exceptions, specifically ArithmeticException when division by zero is attempted.
- If division is successful, the result is printed. If an error occurs, an error message is shown instead.