Community
Participate
Working Groups
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.
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
Closing as invalid
Added InitializationTest#test195
OK, but can it happen?
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.
Reopen to assign
(In reply to comment #2) > Closing as invalid
Verified for 3.5M4
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.