This code:
Double.parseDouble("ABC")
throws a NumberFormatException
.
Why is it wrong to expect a Double.NaN
(NaN is literally Not-A-Number).
A working example is this:
public static void main(String[] args) {
System.out.println("Is ABC a number? " + Double.isNaN(Double.parseDouble("ABC"));
}
I expect Is ABC not a number? true
as output.
Why must this be an Exception?
parse*
methodsparse*
methods have no validNaN
-value. Integer have noNaN
, Float has!(+INF) + (-INF)
or0 / 0
).