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

Integer#parseInt("") throws empty NumberFormatException message

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 22
    • 22
    • core-libs
    • Does not matter, JDK-22-ea+20

    • b21
    • generic
    • generic

      We hit this bug with Lucene tests. When you parse an empty string with Integer#parseInt(""), it thros an NumberFormatException with an empty message.

      Before the change in JDK-8317515 it was correctly using NumberFormatException#forInputString("",..) which creates a correct message like: java.lang.NumberFormatException: For input string: ""

      Reproduce:

      $ ./jdk-17.0.2/bin/jshell
      | Welcome to JShell -- Version 17.0.2
      | For an introduction type: /help intro

      jshell> Integer.parseInt("");
      | Exception java.lang.NumberFormatException: For input string: ""
      | at NumberFormatException.forInputString (NumberFormatException.java:67)
      | at Integer.parseInt (Integer.java:678)
      | at Integer.parseInt (Integer.java:786)
      | at (#1:1)

      $ ./jdk-22/bin/jshell
      | Willkommen bei JShell - Version 22-ea
      | Geben Sie für eine Einführung Folgendes ein: /help intro

      jshell> Integer.parseInt("");
      | Ausnahme java.lang.NumberFormatException:
      | at Integer.parseInt (Integer.java:565)
      | at Integer.parseInt (Integer.java:685)
      | at (#1:1)

      This should be fixed because the exception message is the same since early times of Java and code may rely on this.

      The fix is easy: Replace:
        throw new NumberFormatException("")
      by
        throw NumberFormatException.forInputString("", radix);
      at beginning of method.

            rgiulietti Raffaello Giulietti
            uschindler Uwe Schindler
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: