United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6326693 variable x might already have been assigned, when assignment is in catch block
JDK-6326693 : variable x might already have been assigned, when assignment is in catch block

Details
Type:
Bug
Submit Date:
2005-09-21
Status:
Resolved
Updated Date:
2013-07-11
Project Name:
JDK
Resolved Date:
2013-07-02
Component:
tools
OS:
windows_xp,windows_7
Sub-Component:
javac
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
5.0,7,8
Fixed Versions:

Related Reports

Sub Tasks

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.

                                    

Comments
URL:   http://hg.openjdk.java.net/jdk8/tl/langtools/rev/3b4f92a3797f
User:  vromero
Date:  2013-07-02 21:50:49 +0000

                                     
2013-07-02
URL:   http://hg.openjdk.java.net/jdk8/jdk8/langtools/rev/3b4f92a3797f
User:  lana
Date:  2013-07-05 18:17:02 +0000

                                     
2013-07-05



Hardware and Software, Engineered to Work Together