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

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

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

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

However

 Double.parseDouble(null); // throws java.lang.NullPointerException 
+87
java nullpointerexception exception numberformatexception
May 01 '13 at 19:16
source share
2 answers

It is reasonable to expect that the same exceptions will be thrown for zero; however, these api are very old and cannot be changed at this moment.

and

Since the exception behavior is long-standing and specified in the JavaDoc, it is currently impractical to change the behavior of the method. Closing, as it will not be fixed.

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

Like others, you said: it was probably done by different authors.

+65
May 01 '13 at 19:30
source share

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

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

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

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

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

I would suggest that there are two different authors.

+56
May 01 '13 at 19:26
source share



All Articles