Bug 252771 - Unexpected 'The final local variable obj may already have been assigned' reported
Summary: Unexpected 'The final local variable obj may already have been assigned' repo...
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.5 M4   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-10-30 11:35 EDT by Dani Megert CLA
Modified: 2018-09-10 04:05 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.