In Java, “Exception handling” is an approach that helps us deal with unwanted and probable exceptions. It allows the continuation of execution for other implemented code functionalities. It is such that the mathematical calculations can often involve some terminologies i.e., infinity, etc. that can’t be analyzed by the compiler. As a result, the “ArithmeticException” is faced in Java which halts the execution of the other code as well. Hence, it needs to be eliminated which can be done via the “try/catch” blocks.
This tutorial will discuss the methodologies to cope with Arithmetic Exceptions in Java.
When Can Arithmetic Exceptions be Faced in Java and How to Handle it?
The Arithmetic Exceptions are part of the “java.lang” package and can be faced in the following two scenarios:
- Division of a Number by Zero.
- Division of Non-terminating Big Decimals.
The stated exception in both cases can be handled with the help of the “try/catch” blocks.
Case 1: Faced Arithmetic Exception Upon Dividing a Number by Zero
In this scenario, the “ArithmeticException” is encountered upon the division of a number by zero:
public class ArithmeticException {
public static void main(String[] args) {
int x = 5, y = 0;
int z = x/y;
System.out.println("Division -> "+ z);
}}
In the above code lines:
- Initialize the given integers that are to be divided.
- In the next step, an integer value “5” is divided with “0”.
- This results in returning the “ArithmeticException” limitation since the outcome in this case is infinity.
Output
Solution
To resolve the error in this case, apply the “try/catch” blocks:
public class Exception {
public static void main(String[] args) {
int x = 5, y = 0;
try {
int z = x/y;
System.out.println("Division -> "+ z);
}
catch (java.lang.ArithmeticException e) {
System.out.println(e); System.out.println("Exception Handled!");
}
System.out.println("Code Execution Continues...");
}}
Now, in this code, the division of the discussed numbers is carried out in the “try” block instead. This way, the probable “ArithmeticException” in this block is copied via the included “catch” block that streamlines the code execution.
Output
Case 2: Faced Arithmetic Exception Upon Dividing the Big Decimal Values
In this example, the “ArithmeticException” will be thrown upon the division of the non-terminating big decimals:
import java.math.BigDecimal;
public class Exception2 {
public static void main(String[] argvs) {
BigDecimal val1 = new BigDecimal(382.34);
BigDecimal val2 = new BigDecimal(43.12);
BigDecimal val3 = val1.divide(val2);
System.out.println(val3.toString());
}}
According to this block of code:
- Import the given package to use the “BigDecimal” class.
- After that, create two BigDecimal objects comprising the stated values.
- Now, divide both the big decimals and store the resultant outcome in a separate BigDecimal type value.
- Lastly, retrieve the division of big decimals as a string via the “toString()” method.
Output
This outcome implies that the division of the non-terminating big decimals throws the “ArithmeticException”.
Solution
To fix the error in this situation, likewise, include the “try/catch” blocks in the code:
import java.math.BigDecimal;
public class Exception2 {
public static void main(String[] argvs) {
BigDecimal val1 = new BigDecimal(382.34);
BigDecimal val2 = new BigDecimal(43.12);
try {
BigDecimal val3 = val1.divide(val2); System.out.println(val3.toString());
}
catch (java.lang.ArithmeticException e) {
System.out.println(e);
System.out.println("Exception Handled!");
}
System.out.println("Code Execution Continues...");
}}
Based on the above code implementation, compute the division of the big decimals in the “try” block and similarly, cope with the faced “ArithmeticException” in the “catch” block that eliminates the discussed limitation.
Output
Conclusion
The “Arithmetic Exceptions” can be faced upon dividing a number by zero, or the division of non-terminating big decimals. It can be fixed via the “try/catch” blocks. These blocks then continue to execute the other code functionalities. This article mentioned the cases where the “ArithmeticException” can be faced and provided the solutions to resolving it.