JDK-6326693 : variable x might already have been assigned, when assignment is in catch block
  • Type: Bug
  • Status: Resolved
  • Resolution: Fixed
  • Component: tools
  • Sub-Component: javac
  • Priority: P4
  • Affected Version: 5.0,7,8
  • OS: windows_xp,windows_7
  • CPU: x86
  • Submit Date: 2005-09-21
  • Updated Date: 2013-07-11
  • Resolved Date: 2013-07-02
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
JDK 8
8 b98Resolved
Description
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.