In Java, an ArithmeticException
is a runtime exception that occurs when an exceptional arithmetic condition has occurred. This often arises when an integer division by zero happens. In this tutorial, we’ll cover the basics of ArithmeticException
, common scenarios where it occurs, and how to handle it using examples.
Table of Contents
- What is an
ArithmeticException
? - Common Scenarios Leading to
ArithmeticException
- Handling
ArithmeticException
- Examples
What is an ArithmeticException
?
An ArithmeticException
is thrown to indicate that an arithmetic error has occurred. It’s a subclass of RuntimeException
, so it doesn’t need to be declared in a method or constructor’s throws
clause. The most common reason for this exception is attempting to divide an integer by zero.
Common Scenarios Leading to ArithmeticException
- Division by Zero: The most common scenario is when a number is divided by zero.
- Overflow/Underflow: When an arithmetic operation results in a number larger than
Integer.MAX_VALUE
or smaller thanInteger.MIN_VALUE
.
Handling ArithmeticException
To handle ArithmeticException
, you can use a try-catch block. This allows your program to continue running even after an arithmetic error has occurred.
Examples
Example 1: Integer Division by Zero
This is the simplest example where an ArithmeticException
is thrown.
public class ArithmeticExceptionExample1 {
public static void main(String[] args) {
int a = 10;
int b = 0;
// This line will throw ArithmeticException
int result = a / b;
System.out.println("Result: " + result);
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ArithmeticExceptionExample1.main(ArithmeticExceptionExample1.java:7)
Example 2: Exception Handling with Try-Catch
To handle the ArithmeticException
and prevent the program from crashing, you can use a try-catch block.
public class ArithmeticExceptionExample2 {
public static void main(String[] args) {
int a = 10;
int b = 0;
try {
int result = a / b;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught: Division by zero is not allowed.");
}
System.out.println("Program continues...");
}
}
Output:
ArithmeticException caught: Division by zero is not allowed.
Program continues...
Example 3: Preventing ArithmeticException
You can avoid ArithmeticException
by checking the denominator before performing division.
public class ArithmeticExceptionExample3 {
public static void main(String[] args) {
int a = 10;
int b = 0;
if (b != 0) {
int result = a / b;
System.out.println("Result: " + result);
} else {
System.out.println("Division by zero is not allowed.");
}
System.out.println("Program continues...");
}
}
Output:
Division by zero is not allowed.
Program continues...
Summary
An ArithmeticException
is a common runtime exception in Java that occurs during arithmetic operations, especially division by zero. By understanding how to handle and prevent this exception, you can ensure your programs run smoothly and handle errors gracefully. Use try-catch blocks for handling and conditional checks to prevent such exceptions from occurring.