Why do Double.parseDouble(null) and Integer.parseInt(null) throw different exceptions?

前端 未结 2 1235
暖寄归人
暖寄归人 2020-12-14 05:34

Why do Double.parseDouble(null) and Integer.parseInt(null) throw different exceptions?

Is this a historical accident or intentional? The documentation clearly states

2条回答
  •  天涯浪人
    2020-12-14 05:55

    Note: everything in this post is in the source of Java7-b147

    Double.parseDouble() goes into a Sun library (in sun.misc.FloatingDecimal) the first important thing that happens is:

    in = in.trim(); // don't fool around with white space.
                    // throws NullPointerException if null
    

    Integer.parseInt() is done manually in the Integer class. The first important thing that happens is:

    if (s == null) {
        throw new NumberFormatException("null");
    }
    

    I would guess there are two different authors.

提交回复
热议问题