Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4417678

valueOf() throws inconsistent set of RuntimeExceptions thoughout java API

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 1.3.0
    • core-libs
    • generic
    • generic



      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)
      ======================================================================

            darcy Joe Darcy
            ssultanasunw Shaheen Sultana (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: