Enum: difference of behaviour between exhaustive switch vs. using default:
Gavin Bierman
gavin.bierman at oracle.com
Wed Dec 11 12:20:15 UTC 2024
Could you file this as a bug, and I will take a look?
Thanks,
Gavin
On 10 Dec 2024, at 16:10, Jean-Noël Rouvignac (ForgeRock) <jean-noel.rouvignac at pingidentity.com> wrote:
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/20241211/fbe01320/attachment-0001.htm>
More information about the amber-dev
mailing list