Java Program for Div Zero Exception
Program
public class DivZero {
public static void main(String[] args){
int a, b;
a = 10;
b = 0;
System.out.println(a/b);
}
}
This Java program demonstrates an ArithmeticException: Division by Zero, which occurs when a number is divided by zero.
-
Variables
a
andb
:- The program declares two integers,
a
andb
. a
is assigned a value of10
.b
is assigned a value of0
.
- The program declares two integers,
-
Division Operation (
a / b
):- The program attempts to divide
a
byb
using the statementSystem.out.println(a/b);
. - Since dividing a number by zero is mathematically undefined, Java throws an ArithmeticException.
- The program attempts to divide
-
No Exception Handling:
- This program does not include any error-handling mechanism (e.g.,
try-catch
block). - As a result, the program terminates abnormally and prints an error message.
- This program does not include any error-handling mechanism (e.g.,
-
ArithmeticException:
- This is a runtime exception in Java.
- It occurs when an invalid arithmetic operation (e.g., division by zero) is performed.
Output
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ThrowDivZero.main(ThrowDivZero.java:6)
- The exception message indicates:
- ArithmeticException is the type of error.
- The error occurs at line 6 (
System.out.println(a/b);
). - The reason is division by zero (
/ by zero
).