JDK-4787924 : Integer.parseInt() and Double.parseDoube() throw different exceptions on null
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.1,7
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_2000,windows_xp,windows_7
  • CPU: x86
  • Submitted: 2002-12-03
  • Updated: 2003-10-22
  • Resolved: 2003-10-22
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Description

Name: nt126004			Date: 12/03/2002


FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

FULL OPERATING SYSTEM VERSION :
MS Windows 2k

5.00.2195

A DESCRIPTION OF THE PROBLEM :
When passed a null value, Integer.parseInt() throws a NumberFormatException.
However, Double.parseDouble() and Float.parseFloat() throw NullPointerException
when passed null.


EXPECTED VERSUS ACTUAL BEHAVIOR :
I expected to get the same exception (NullPointerException or
NumberFormatException) in both cases

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class test {
	public static int getInt(String value, int defaultValue) {
        try {
            return Integer.parseInt(value);
        } catch(Exception nfe) {
			nfe.printStackTrace();
            return defaultValue;
        }
    }

    public static float getFloat(String value, float defaultValue) {
        try {
            return Float.parseFloat(value);
        } catch(Exception nfe) {
			nfe.printStackTrace();
            return defaultValue;
        }
    }

    public static void main(String[] args) {
        System.out.println(getInt(null, -1));
        System.out.println(getFloat(null, -1));
    }
}
---------- END SOURCE ----------
(Review ID: 178469) 
======================================================================

Comments
EVALUATION 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. Will consider for Tiger. ###@###.### 2002-12-03 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. ###@###.### 2003-10-21
21-10-2003