Bug 252771

Summary: Unexpected 'The final local variable obj may already have been assigned' reported
Product: [Eclipse Project] JDT Reporter: Dani Megert <daniel_megert>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: jerome_lanneluc, mauromol, philippe_mulet
Version: 3.5   
Target Milestone: 3.5 M4   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Dani Megert CLA 2008-10-30 11:35:32 EDT
I20081029-1823.

Have this code:

	final Object obj;
	try {
		if (<some condition>)
			obj= new Object();
		else
			obj= new Object();
	} catch (ArithmeticException e) {
		obj= new Object();
	}

==> I get an error saying: The final local variable obj may already have been assigned. This is not true.
Comment 1 Philipe Mulet CLA 2008-10-30 12:55:38 EDT
This behavior is expected from JLS 16.2.15 and the fact that ArithmeticException is a runtime exception; and btw javac agrees with us:

X.java:10: variable obj might already have been assigned
                        obj= new Object();
                        ^
1 error
Comment 2 Philipe Mulet CLA 2008-10-30 12:56:16 EDT
Closing as invalid
Comment 3 Philipe Mulet CLA 2008-10-30 12:59:39 EDT
Added InitializationTest#test195
Comment 4 Dani Megert CLA 2008-10-30 13:29:44 EDT
OK, but can it happen?
Comment 5 Philipe Mulet CLA 2008-10-31 06:47:24 EDT
After the assignment, we issue a branch instruction to the end of the try/catch; in theory any instruction could cause an unchecked exception to be fired, and possibly an ArithmeticException; though you'd be extremely unlucky. <g>

The language spec is too much defensive here, but this is what it takes to ensure no possible evil code is tampering with 'final' rules.
Comment 6 Jerome Lanneluc CLA 2008-12-08 05:18:18 EST
Reopen to assign
Comment 7 Jerome Lanneluc CLA 2008-12-08 05:19:21 EST
(In reply to comment #2)
> Closing as invalid


Comment 8 Jerome Lanneluc CLA 2008-12-09 06:56:18 EST
Verified for 3.5M4
Comment 9 Mauro Molinari CLA 2018-09-10 04:05:40 EDT
Hi,
before opening a new bug report, I was wondering if the same applies to the following case, where a *checked* exception is thrown:

final MessageDigest digest;
try {
  digest = MessageDigest.getInstance("SHA-256");
} catch (final NoSuchAlgorithmException e) {
  digest = null;
}

Here the error is given at "digest = null", but I can't think of any way in which it may have already been assigned.