In contrast to integer arithmetic, which does not have designated infinity or NaN values, throwing exceptions can be a helpful approach. To address this, IEEE 754 specifies that Infinity is represented by positive infinity and -Infinity by negative infinity, while NaN is used for undefined values. Additionally, floating point values also have their own infinity and NaN representations, with NaN being used for undefined values.

Question:

What’s preventing this code from throwing an

ArithmeticException

error? Check it out.

```
public class NewClass {
public static void main(String[] args) {
// TODO code application logic here
double tab[] = {1.2, 3.4, 0.0, 5.6};
try {
for (int i = 0; i < tab.length; i++) {
tab[i] = 1.0 / tab[i];
}
} catch (ArithmeticException ae) {
System.out.println("ArithmeticException occured!");
}
}
}
```

I have no idea!

Solution 1:

Infinity and -Infinity are defined as per IEEE 754, while NaN is also defined as per the same standard.

Furthermore, it is worth noting that floating point values possess

-0.0

, which means that

1.0/ -0.0

is equivalent to

-Infinity

.

None of these values exist in integer arithmetic, resulting in the throwing of an Exception.

To identify any values that could result in a non-finite number, such as NaN, 0.0, or -0.0, you can perform the following check.

```
if (Math.abs(tab[i] = 1 / tab[i]) < Double.POSITIVE_INFINITY)
throw new ArithmeticException("Not finite");
```

Solution 2:

Why not examine it on your own and raise an exception if that’s your intention?

```
try {
for (int i = 0; i < tab.length; i++) {
tab[i] = 1.0 / tab[i];
if (tab[i] == Double.POSITIVE_INFINITY ||
tab[i] == Double.NEGATIVE_INFINITY)
throw new ArithmeticException();
}
} catch (ArithmeticException ae) {
System.out.println("ArithmeticException occured!");
}
```

Solution 3:

When working with floating point numbers, dividing by zero results in

Infinity

, also referred to as

NaN

(which means “not a number”).

To avoid any potential issues, it is advisable to test

tab[i]

prior to its usage. In case of necessity, you may throw an exception on your own.

Solution 4:

The double literal 0.0 is not regarded as absolute zero since it is believed that the double variable can hold values that are close to infinity without any exceptions.