The following snippet throws an exception at runtime, because of the missing break stmt. I was wondering if the compiler couldn’t help here and warn about the missing break stmt.
public class T1 {
public static void main(String argv[]) {
record Point(int x, int y) {}
Point p = new Point(1, 3);
switch (p) {
case Point(var x, var y) when y == 2:
System.out.println("Value of x = " + x); // <<- line 10
break;
case Point(var x, var y) :
System.out.println("Value of y = " + y);
}
}
}
The exception occurs at line 10 - which makes it even less obvious to figure out. Output is:
Value of y = 3
Exception in thread "main" java.lang.MatchException
at T1.main(T1.java:10)
-----
Command line used:
~/java/jdk21/jdk1.21.0/bin/javac --enable-preview --release 21 T1.java
~/java/jdk21/jdk1.21.0/bin/java --enable-preview T1
public class T1 {
public static void main(String argv[]) {
record Point(int x, int y) {}
Point p = new Point(1, 3);
switch (p) {
case Point(var x, var y) when y == 2:
System.out.println("Value of x = " + x); // <<- line 10
break;
case Point(var x, var y) :
System.out.println("Value of y = " + y);
}
}
}
The exception occurs at line 10 - which makes it even less obvious to figure out. Output is:
Value of y = 3
Exception in thread "main" java.lang.MatchException
at T1.main(T1.java:10)
-----
Command line used:
~/java/jdk21/jdk1.21.0/bin/javac --enable-preview --release 21 T1.java
~/java/jdk21/jdk1.21.0/bin/java --enable-preview T1