JDK-4417678 : valueOf() throws inconsistent set of RuntimeExceptions thoughout java API
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2001-02-22
  • Updated: 2003-10-22
  • Resolved: 2003-10-22
Related Reports
Relates :  
Description

Name: ssT124754			Date: 02/21/2001


* don't reject the problem right away even if it 1.3 please *
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


Is it a bug, a lack of discipline or maybe something else? I don't know but the
exceptions thrown by the various implementation of valueOf(String) among core
java classes are not consistent (they don't react the same way when a null is
passed to the method for example).

I think it would be nice to see some overall consistency among the
implementation of valueOf(String) among those basic classes (even though it is
probably to late for Sun to fix it and we'll have to live with those like a few
other decifiencies like that). Well, let us know.

Source code:

public class ValueOf {

  public static void main(String[] argv) {

    try {
      Boolean.valueOf(null);
    } catch (Exception e) {
      System.out.println("Boolean.valueOf(null)=" + e.toString());
    }

    try {
      Boolean.valueOf("xyz");
    } catch (Exception e) {
      System.out.println("Boolean.valueOf(\"xyz\")=" + e.toString());
    }

    try {
      Byte.valueOf(null);
    } catch (Exception e) {
      System.out.println("Byte.valueOf(null)=" + e.toString());
    }

    try {
      Byte.valueOf("xyz");
    } catch (Exception e) {
      System.out.println("Byte.valueOf(\"xyz\")=" + e.toString());
    }

    try {
      Short.valueOf(null);
    } catch (Exception e) {
      System.out.println("Short.valueOf(null)=" + e.toString());
    }

    try {
      Short.valueOf("xyz");
    } catch (Exception e) {
      System.out.println("Short.valueOf(\"xyz\")=" + e.toString());
    }

    try {
      Integer.valueOf(null);
    } catch (Exception e) {
      System.out.println("Integer.valueOf(null)=" + e.toString());
    }

    try {
      Integer.valueOf("xyz");
    } catch (Exception e) {
      System.out.println("Integer.valueOf(\"xyz\")=" + e.toString());
    }

    try {
      Long.valueOf(null);
    } catch (Exception e) {
      System.out.println("Long.valueOf(null)=" + e.toString());
    }

    try {
      Long.valueOf("xyz");
    } catch (Exception e) {
      System.out.println("Long.valueOf(\"xyz\")=" + e.toString());
    }

    try {
      Float.valueOf(null);
    } catch (Exception e) {
      System.out.println("Float.valueOf(null)=" + e.toString());
    }

    try {
      Float.valueOf("xyz");
    } catch (Exception e) {
      System.out.println("Float.valueOf(\"xyz\")=" + e.toString());
    }

    try {
      Double.valueOf(null);
    } catch (Exception e) {
      System.out.println("Double.valueOf(null)=" + e.toString());
    }

    try {
      Double.valueOf("xyz");
    } catch (Exception e) {
      System.out.println("Double.valueOf(\"xyz\")=" + e.toString());
    }

    try {
      java.sql.Date.valueOf(null);
    } catch (Exception e) {
      System.out.println("java.sql.Date.valueOf(null)=" + e.toString());
    }

    try {
      java.sql.Date.valueOf("xyz");
    } catch (Exception e) {
      System.out.println("java.sql.Date.valueOf(\"xyz\")=" + e.toString());
    }

  }

}

Output: *note that Boolean.valueOf(null) is not throwing an exception*
Byte.valueOf(null)=java.lang.NumberFormatException: null
Byte.valueOf("xyz")=java.lang.NumberFormatException: xyz
Short.valueOf(null)=java.lang.NumberFormatException: null
Short.valueOf("xyz")=java.lang.NumberFormatException: xyz
Integer.valueOf(null)=java.lang.NumberFormatException: null
Integer.valueOf("xyz")=java.lang.NumberFormatException: xyz
Long.valueOf(null)=java.lang.NumberFormatException: null
Long.valueOf("xyz")=java.lang.NumberFormatException: xyz
Float.valueOf(null)=java.lang.NullPointerException
Float.valueOf("xyz")=java.lang.NumberFormatException: xyz
Double.valueOf(null)=java.lang.NullPointerException
Double.valueOf("xyz")=java.lang.NumberFormatException: xyz
java.sql.Date.valueOf(null)=java.lang.IllegalArgumentException
java.sql.Date.valueOf("xyz")=java.lang.IllegalArgumentException
(Review ID: 117428) 
======================================================================

Comments
WORK AROUND Name: ssT124754 Date: 02/21/2001 Well, use your own wrappers which would know how to handle those differences in a nice way, or learn what is their behavior. ======================================================================
11-06-2004

EVALUATION Here's a complete list of classes in the jdk which contain valueOf() methods. ./java/lang/Double.java: public static Double valueOf(String s) throws NumberFormatException { ./java/lang/Boolean.java: public static Boolean valueOf(boolean b) { ./java/lang/Boolean.java: public static Boolean valueOf(String s) { ./java/lang/Long.java: public static Long valueOf(String s, int radix) throws NumberFormatException { ./java/lang/Long.java: public static Long valueOf(String s) throws NumberFormatException ./java/lang/Float.java: public static Float valueOf(String s) throws NumberFormatException { ./java/lang/Integer.java: public static Integer valueOf(String s, int radix) throws NumberFormatException { ./java/lang/Integer.java: public static Integer valueOf(String s) throws NumberFormatException ./java/lang/Byte.java: public static Byte valueOf(String s, int radix) ./java/lang/Byte.java: public static Byte valueOf(String s) throws NumberFormatException { ./java/lang/String.java: public static String valueOf(Object obj) { ./java/lang/String.java: public static String valueOf(char data[]) { ./java/lang/String.java: public static String valueOf(char data[], int offset, int count) { ./java/lang/String.java: public static String valueOf(boolean b) { ./java/lang/String.java: public static String valueOf(char c) { ./java/lang/String.java: public static String valueOf(int i) { ./java/lang/String.java: public static String valueOf(long l) { ./java/lang/String.java: public static String valueOf(float f) { ./java/lang/String.java: public static String valueOf(double d) { ./java/lang/Short.java: public static Short valueOf(String s, int radix) ./java/lang/Short.java: public static Short valueOf(String s) throws NumberFormatException { ./java/sql/Date.java: public static Date valueOf(String s) { ./java/sql/Time.java: public static Time valueOf(String s) { ./java/sql/Timestamp.java: public static Timestamp valueOf(String s) { ./java/math/BigInteger.java: public static BigInteger valueOf(long val) { ./java/math/BigDecimal.java: public static BigDecimal valueOf(long unscaledVal, int scale) { ./java/math/BigDecimal.java: public static BigDecimal valueOf(long val) { While it may not be possible or desirable to change the current behaviour of these methods due to compatibility constraints, we should be aware of the differences that exist particularly with respect to the wrapper classes. -- iag@sfbay 2001-10-23 Will examine these appearant API inconsistencies. ###@###.### 2002-04-24 Changes in exceptions thrown is a change more appropriately considered for a major release; recommitting bug to Tiger. ###@###.### 2002-11-11 Impractical to change the exception hehavior of these methods at this time due to compatibility concerns. Closing as will not fix. ###@###.### 2003-10-21
11-11-2002