Reachability and null analysis of assignment inside a try block with a throw inside the catch block

Nir Lisker nlisker at
Wed Oct 31 21:42:01 UTC 2018


I hope this is the correct list for this question. It is the result of a
short discussion with the Eclipse compiler maintainers [1].

Given the following code:

    A getA() {
        A a = null;
        try {
            a = new A();
        } catch (ExceptionThrownByConstructor e) {
            throw e;
        if (a != null) {
            return a;
        } else {

If I understood the JLS correctly, the try-catch statement will either
assign a non-null value to 'a' and continue, or will not continue (either
rethrowing the checked exception or an unchecked exception occurs). If so,
by the time the if condition is evaluated, 'a' is surely not null, so the
else block should be unreachable, but it is not marked as such.
Furthermore, removing the else block gives a compiler error that the method
must return an object of type A, but this condition is met.

I understand that the whole if-else statement can be removed and I can just
'return a', but that's outside of my point.

Am I wrong? Shouldn't the if condition be treated as a constant expression
('true' in this case)?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the compiler-dev mailing list