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

variable x might already have been assigned, when assignment is in catch block

XMLWordPrintable

    • b98
    • x86
    • windows_xp, windows_7

      FULL PRODUCT VERSION :
      java version "1.5.0_05"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      If a variable is declared final but is not assigned a value, compilation fails if the variable is assigned different values in different catch blocks, and the only assignments occur in the catch blocks.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      This was not working in 1.4.2, but it did work in earlier versions, including 1.4.1_01, 1.3.1_04, and 1.1.8. There's no option in the bug report form for JDK earlier than 1.4.2.

        To reproduce, declare a final variable but do not assign it a value. Write a try/catch block with multiple catch blocks, and in each catch block, assign the variable. The compile will fail with the message "variable x might already have been assigned". If only one catch block is present, no compile error occurs.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compilation should succeed if a final variable can be assigned only once in multiple catch blocks and no previous assignment was made.
      ACTUAL -
      Compilation fails with message "variable x might already have been assigned"

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      variable x might already have been assigned

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.IOException;

      public class TestFinals
      {
      public void function()
      {
      final String v;
      try {
      doSomething();
      } catch ( IOException e ) {
      v = "value1";
      } catch ( InterruptedException ie ) {
      v = "value2";
      }
      }

      private void doSomething() throws IOException, InterruptedException
      {
      boolean a = true;
      boolean b = false;

      if ( a )
      throw new IOException();
      if ( b )
      throw new InterruptedException();
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Remove the final modifier from the variable declaration.

            vromero Vicente Arturo Romero Zaldivar
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: