I have the following code:
public static void main(String[] args) {
try {
int d1 = 3;
int d2 = 0;
int d = d1/d2;
} catch (Exception ex) {
System.out.println("Exception");
}
}
When this code is run, it is obvious that exception will occur. However, if I change the code as follows:
public static void main(String[] args) {
try {
double d1 = 3;
double d2 = 0;
double d = d1/d2;
} catch (Exception ex) {
System.out.println("Exception");
}
}
Then the exception does not throw. I really don't get it. Can anyone elaborate on that, please?
Because divide a double by 0.0 will produce NAN or +/- infinity, not an exception.
When you perform integer division by 0 you can get an exception as there is no defined behaviour for this.
There is a defined behaviour for double division in the IEEE standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With