class C {
void alwaysThrow1(String s) {
switch (s) {
case "a" -> throw new IllegalArgumentException();
default -> throw new IllegalStateException();
}
System.out.println();
}
void alwaysThrow2(Integer i) {
switch (i) {
case Integer integer when true:
throw new IllegalArgumentException();
}
System.out.println(42);
}
void breakFromEndlessLoop() {
EndlessLoop:
for (;;) {
switch ((int)Math.random()) {
case 1 -> {break EndlessLoop;}
default -> throw new RuntimeException();
}
}
System.out.println();
}
void continueEndlessLoop() {
EndlessLoop:
for (;;) {
switch ((int)Math.random()) {
case 1 -> {continue EndlessLoop;}
default -> throw new RuntimeException();
}
}
System.out.println();
}
void endlessLoopsInAllBranches(String s) {
switch (s) {
case "a" -> { while(true); }
default -> { for(;;); }
}
System.out.println();
}
void endlessLoopInBranch(String s) {
switch (s) {
case "a" -> { while(true); }
default -> {}
};
System.out.println();
}
void endlessLoopInBranchWithValueBreak(String arg) {
int result = switch (arg) {
case "one" -> { while(true); yield 1;}
default -> 0;
};
System.out.println(result);
}
int returnBeforeEnhancedSwitchStatement(String s) {
return 2;
switch(s) {
case "a" -> {return 1;}
default -> {return 0;}
}
}
int returnBeforeSwitchExpressionInInitializer(String s) {
return 2;
int n = switch(s) {
case "a" -> 1;
default -> 0;
};
}
int returnBeforeSwitchExpressionInAssignment(String s) {
int n;
return 2;
n = switch(s) {
case "a": n = 1; yield 1;
default: n = 0; yield 0;
};
}
int returnSwitchExpression(String s) {
return switch(s) {
case "a" -> 1;
default -> 0;
};
System.out.println();
}
void switchExpressionUnreachableInFinally(int n) {
String s;
try {
} finally {
return;
s = switch (n) {
case 0 -> "a";
default -> "b";
};
}
}
void switchExpressionReachableInFinally(int n) {
String s;
try {
return;
} finally {
s = switch (n) {
case 0 -> "a";
default -> "b";
};
}
}
static class SwitchExpressionReturnedFromTry {
int foo(String s) throws Exception {
try {
return switch (s) {
case "a" -> bar(1);
default -> bar(0);
};
} finally {
System.out.println("b");
}
System.out.println("c");
}
int bar(int i) throws Exception { return i; }
}
static class ValueBreakSwitchExpressionReturnedFromTry {
int foo(String s) throws Exception {
try {
return switch (s) {
case "a": yield bar(1);
default: yield bar(0);
};
} finally {
System.out.println("b");
}
System.out.println("c");
}
int bar(int i) throws Exception { return i; }
}
static class SwitchStatementReturnsFromTry {
int foo(String s) throws Exception {
try {
switch (s) {
case "a" -> { return bar(1); }
default -> { return bar(0); }
}
} finally {
System.out.println("b");
}
System.out.println("c");
}
int bar(int i) throws Exception { return i; }
}
}