Switch expressions were introduced by JEP 361 and reduce the verbosity of common usages of switch.
As a particular detail of this feature, writing a default class for a switch expression over an enum is not necessary. The following code compiles:
public enum MyEnum {
ONE,
TWO;
}
public class OtherClass {
public static String computeAttribute(MyEnum myEnum) {
return switch (myEnum) {
case ONE -> "one";
case TWO -> "two";
};
}
}
The switch expression is exhaustive at the moment, but if the enum is recompiled separately, the new version of the enum may include another entry (e.g. THREE) which the switch statement does not anticipate.
Both the JEP and this post mention that the compiler inserts a default clause which throws an exception to indicate this disharmony.
I could not find anything which notes the exact type of the exception thrown. What is the exception thrown by default clauses inserted by the compiler in switch expressions over enum values?