Enum: difference of behaviour between exhaustive switch vs. using default:

Jean-Noël Rouvignac (ForgeRock) jean-noel.rouvignac at pingidentity.com
Tue Dec 10 16:10:57 UTC 2024


Hello amber-dev experts!

I am modernizing our codebase by making it use enhanced / exhaustive
switches.

In several places, I replaced `default: ` by `case
THE_ONLY_UNUSED_ENUM_VALUE:`, except that I am hitting an unexpected
difference in behaviour, at least from my point of view.

I have reduced the code to the following reproducer (tested on the
https://dev.java/playground/), where `main1()` compiles, but `main2()` does
not. And yet, I am under the impression both should be equivalent?

What do you think?
Thanks a lot.



import java.io.IOException;

class Main {
    public enum Action { IGNORE, REJECT }

    public static void main(String[] args) {
        main1();
        main2();
    }

    private static void main1() {
        String s;
        try {
            s = getValue();
        } catch (IOException e) {
            switch (getAction()) {
                case IGNORE:
                    return;
                default:
                    throw new RuntimeException("REJECTED");
            }
        }

        System.out.println(s);
    }

    private static void main2() {
        String s;
        try {
            s = getValue();
        } catch (IOException e) {
            switch (getAction()) {
                case IGNORE:
                    return;
                case REJECT: // <------------------- Fails compilation
                    throw new RuntimeException("REJECTED");
            }
        }

        System.out.println(s); // <------- Main.java:40: error: variable s
might not have been initialized
    }

    static Action getAction() {
        return Action.IGNORE;
    }

    static String getValue() throws IOException {
        return "SUCCESS";
    }
}

-- 
_CONFIDENTIALITY NOTICE: This email may contain confidential and privileged 
material for the sole use of the intended recipient(s). Any review, use, 
distribution or disclosure by others is strictly prohibited.  If you have 
received this communication in error, please notify the sender immediately 
by e-mail and delete the message and any file attachments from your 
computer. Thank you._
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20241210/904de073/attachment.htm>


More information about the amber-dev mailing list