Let's look at the following code snippet in Java.
package division;
import java.math.BigDecimal;
final public class Main
{
public static void main(String[] args)
{
BigDecimal a = new BigDecimal(2);
BigDecimal b = new BigDecimal(3);
System.out.println(a.multiply(b));
System.out.println(a.add(b));
System.out.println(b.subtract(a));
System.out.println(a.divide(b));
}
}
In the above code snippet, all of the operations except the last one (division) are performed successfully. An attempt to divide two BigDecimal
numbers in Java throws the java.lang.ArithmeticException
. Why? What is the solution to this problem?