Sub topic 13
When a switch throws MatchException
This chapter assumes Java classes, interfaces, inheritance, switch expressions, instanceof, sealed types, records, and basic generics. You should use JDK 21 or newer. MatchException is a runtime failure related to exhaustive pattern switches.
What MatchException means
An exhaustive switch claims that every possible value is covered. For example, a switch over a sealed interface can list every permitted subtype:
sealed interface Shape permits Circle, Rectangle {}
record Circle(double radius) implements Shape {}
record Rectangle(double width, double height) implements Shape {}
static double area(Shape shape) {
return switch (shape) {
case Circle c -> Math.PI * c.radius() * c.radius();
case Rectangle r -> r.width() * r.height();
};
}The compiler checks this switch using the type information available when it compiles the code. Normally, one of the two cases always matches. If the runtime value does not match any case, Java throws java.lang.MatchException.
MatchException is unchecked, so you do not need to declare or catch it. It usually indicates that compiled code and runtime type information no longer agree.
How the mismatch happens
The most common cause is separate compilation. One library version is compiled first. Later, another version changes an enum or sealed hierarchy. The old switch class might still be running with the new type information.
An exhaustive enum switch can have the same problem:
enum Status {
NEW, DONE
}
public class StatusLabels {
static String label(Status status) {
return switch (status) {
case NEW -> "not started";
case DONE -> "finished";
};
}
public static void main(String[] args) {
System.out.println(label(Status.NEW));
}
}Compile and run this complete example with JDK 21 or newer:
javac StatusLabels.java
java StatusLabelsIt prints:
not startedIf a later version of Status adds another constant, such as CANCELLED, while the already-compiled StatusLabels class is reused, the old switch has no matching case for that value. It can then throw MatchException.
| Runtime situation | Typical result |
|---|---|
| A listed case matches | The case result is used |
| No case matches in an exhaustive switch | `MatchException` |
| The selector is `null` and there is no `case null` | `NullPointerException` |
| A `default` case matches | The default result is used |
MatchException is different from null failure
A null selector is handled separately. Pattern switches do not match null unless you write an explicit label:
static String describe(Object value) {
return switch (value) {
case null -> "no value";
case String s -> "text";
default -> "other";
};
}Without case null, a null selector normally causes NullPointerException, not MatchException. A default label also does not replace case null; default does not match null in this situation.
Choosing between exhaustive cases and default
An exhaustive switch without default gives the compiler more information. If you add a permitted subtype or enum constant and recompile, the compiler can identify switches that need updating.
A default case is useful when unknown future values should receive a deliberate fallback:
static String label(Status status) {
return switch (status) {
case NEW -> "not started";
case DONE -> "finished";
default -> "unrecognized status";
};
}This prevents a missing-match failure, but it can also hide a changed domain model. Use default when fallback behavior is part of the design, not merely to silence an exhaustiveness error.
Exercise
Why can an exhaustive switch over an enum throw MatchException after the enum library is upgraded, even though the source switch was exhaustive when compiled?
Show answer
The compiler checked the enum constants known at compile time. A newer runtime enum can contain a constant for which the old compiled switch has no case.