Consider debugging the code with jdk 17:
public class DebuggingSwitch {
public static void main(String[] args) throws Exception {
String returnValue;
try {
System.out.println("Enter the try catch block");
throw new Exception("Error");
} catch(Exception e) {
returnValue = switch(e.getMessage()) { // breakpoint here and start stepping over
case "Error" -> {
System.out.println("Error exception"); // 1 stop - ok
yield "Error"; // 2 stop - ok
}
case "Warning" -> {
System.out.println("Warning exception");
yield "Warning";
}
default -> {
System.out.println("Default exception");
System.out.println("Another default exception");
throw e; // 2 stop - NOT OK
}
};
}
}
}
Set a breakpoint at the switch line inside the catch block, then step over several times. On the third stop it will stop at the line with "throw e;" inside the default block which is definitely not executed, which is really misleading.
Originally the problem was reported here: https://youtrack.jetbrains.com/issue/IDEA-316450