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

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


Hi,

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)?

Thanks,
Nir

[1] https://bugs.eclipse.org/bugs/show_bug.cgi?id=540532
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20181031/ac087eb1/attachment.html>


More information about the compiler-dev mailing list