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

JavaExceptionNullpointerexceptionNumberformatexception

Java Problem Overview


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

Is this a historical accident or intentional? The documentation clearly states two types of exceptions for Double.parseDouble(...) and one for Integer.parseInt(), but it seems inconsistent:

Integer.parseInt(null); // throws java.lang.NumberFormatException: null

However

Double.parseDouble(null); // throws java.lang.NullPointerException

Java Solutions


Solution 1 - Java

> It is reasonable to expect the same exceptions to be thrown for null; however, these api's are very old and may not be able to be changed at this point.

And:

> Since the exception behavior is long-standing and specified in the JavaDoc, it is impractical to change either method's behavior at this time. Closing as will not fix.

As taken from: Bug Report: Integer.parseInt() and Double.parseDouble() throw different exceptions on null.

Like others have stated: It's likely made by different authors.

Solution 2 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpho79View Question on Stackoverflow
Solution 1 - JavaFloris VellemanView Answer on Stackoverflow
Solution 2 - Javadurron597View Answer on Stackoverflow