Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8304694

Runtime exception thrown when break stmt is missing

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 21
    • 20, 21
    • tools
    • None
    • b16

      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

            jlahoda Jan Lahoda
            mtrudeau Michel Trudeau
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: