eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
156 stars 123 forks source link

Bug 466406 - [compile] valid final variable causes eclipse compilation error when set via multiple catch blocks #2726

Closed stephan-herrmann closed 1 month ago

stephan-herrmann commented 1 month ago

Example from https://bugs.eclipse.org/466406:

public class ExceptionTest {

    public void doStuff() throws Exception {
    }

    public void test() {
        final Exception ex;
        try {
            doStuff();
            return;
        } catch (NullPointerException e) {
            ex = e;
        } catch (Exception e) {
            // compile error here, only in the IDE
            ex = e;
        }
        ex.printStackTrace();
    }
}

Bogus error reported is:

2. ERROR in /tmp/ExceptionTest.java (at line 16)
        ex = e;
        ^^
The final local variable ex may already have been assigned

In bugzilla I pondered if there might be a spec change behind, but §16.2.5 is pretty clear in this regard since at least SE 7: definite assignment in one catch blocks cannot influence a sibling catch block of the same try statement.

stephan-herrmann commented 1 month ago

Interestingly, null flow analysis does not see any connection between catch blocks:

    public void test() {
        Exception ex = null;
        try {
            doStuff();
            return;
        } catch (NullPointerException e) {
            ex = e;
        } catch (Exception e) {
            ex.printStackTrace(); // Null pointer access: The variable ex can only be null at this location
            ex = e;
        }
        ex.printStackTrace();
    }