Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

Exhaustiveness checking moves some errors from run time to compile time, but couples every switch to the full set of variants. How do you set org-wide guidance on using exhaustive switches versus default branches?

Inside a module or one team's code, we prefer exhaustive switches without a default, so that adding a variant makes the compiler list every place to update. At boundaries where data comes from other teams or external systems, we convert it into our own sealed model at parse time and handle unknown values there, once. We avoid publishing sealed hierarchies that many independent teams switch over unless we control their release cadence, write the policy into API guidelines and lint rules, and review its cost after a year.

Why this question is asked

Exhaustiveness checking is one of the most useful things modern Java gives us, and also one of the easiest to misuse at scale. A switch with no default turns a forgotten case into a compile error, which is wonderful inside one codebase and painful when a library owned by another team adds a variant and fifty services stop building. Interviewers ask this at a principal level because the answer is not a language rule but a policy: where the trade-off flips, and how to make an organization apply it consistently. In this answer, we will recap the trade-off, show the two places where the rule differs, explain how to handle shared hierarchies, and describe how to write the guidance down and check that it works.

1A Quick Recap of the Trade-off

A switch over a sealed type, a type whose permitted subtypes are listed with permits, is exhaustive when it has a case for every subtype. Then no default is needed. If someone adds a subtype, every exhaustive switch over that type fails to compile until it handles the new case. That is the benefit: the compiler finds all the places that need a decision.

A default branch does the opposite. The switch keeps compiling when a variant is added, and the new variant silently goes to the default. That protects us from build breaks, but it moves the mistake to run time, where it might be a wrong discount, a dropped message, or a status shown as unknown for months before anyone notices.

So the question is not which one is better. It is where we want an unexpected variant to be noticed: at compile time by the people who wrote the switch, or at run time by whoever handles the default.

2Rule 1: Inside a Module, No Default

Within a module, or across modules owned and released by one team, a compile break is cheap. The person adding the variant sees every switch that needs updating in the same change, fixes them, and ships one consistent release. Here we want exhaustive switches without default, every time.

Java
sealed interface OrderEvent permits Placed, Paid, Shipped {}
String label(OrderEvent e) {
return switch (e) {
case Placed p -> "placed";
case Paid p -> "paid";
case Shipped s -> "shipped";
}; // adding Refunded breaks this line, which is what we want
}

Notice that a default here would only hide work we must do anyway. If Refunded is added, the label really does need a decision, and the compiler is the cheapest reviewer we have.

3Rule 2: At Boundaries, Convert Once and Handle Unknowns There

Data from other teams' services, partners, or old clients is different. We do not control when a new value appears, and it can appear at run time, in a JSON field or a Kafka message, with no compile step at all. A default branch scattered through our code is a poor answer, because each switch then makes its own silent decision.

The better pattern is an anti-corruption step at the edge. When a message arrives, we parse it into our internal sealed model in one place. An unknown event type is handled right there, explicitly: mapped to a known variant if the business rule is clear, mapped to a dedicated Unknown variant that carries the raw data, or rejected and sent to a dead-letter queue with an alert. Everything after the parser works on a closed set and stays exhaustive.

Java
OrderEvent parse(JsonNode n) {
return switch (n.get("type").asText()) {
case "PLACED" -> new Placed(n.get("id").asText());
case "PAID" -> new Paid(n.get("id").asText());
case "SHIPPED" -> new Shipped(n.get("id").asText());
default -> throw new UnknownEventException(n); // the only default, at the edge
};
}
Step throughWhere defaults belong
  1. External inputJSON, Kafka, partner APIs: values can be anything
  2. Parser at the edgethe one default: map, wrap as Unknown, or reject
  3. Internal sealed modela closed set of records
  4. Business logicexhaustive switches, no default

4 layers, from the top down. Step through them one at a time.

Unknown values are handled once, at the top, so the code below stays exhaustive.

Notice that the only default in the flow is on a String, at the boundary, and that it is loud. That is one place to monitor and one place to change when a partner announces a new value.

4Shared Hierarchies Across Many Teams

The hard case is a sealed hierarchy published in a shared library and switched over by many independent teams. If the platform team adds a variant, every consumer that upgrades gets compile errors, and every consumer that does not upgrade but receives the new type at run time gets a MatchException from the synthetic default the compiler inserted. Either way, a change in one team forces work in many.

So the guidance is to avoid exposing sealed hierarchies for open-ended switching across teams unless we control the release cadence, for example a monorepo where all consumers build together. Where we must share, we can publish a non-sealed interface with an accessor such as kind() returning a string, plus the sealed model for internal use; or we can commit to a versioning rule that a new variant is a major version with a deprecation period. The point is that adding a variant to a widely switched type is a breaking API change and should be treated as one.

Step throughChoosing default or exhaustive for one switch
  1. Is the data from outside our release unit?
    YesConvert at the edge with one explicit defaultNoContinue
  2. Is the sealed type shared with other teams?
    YesTreat new variants as breaking API changesNoContinue
  3. Switch exhaustively with no defaultthe compiler finds every place to update

3 steps in order. Play them, or step through them one at a time.

5Writing It Down and Checking It

A policy nobody can find is not a policy. We put the rule in the API design guidelines with the examples above, and then automate what we can. Static analysis tools such as Error Prone or a custom ArchUnit test can flag a default branch in a switch over a sealed type inside the core packages, and can require that parser classes at the edge are the only place such defaults appear. Code review then focuses on the exceptions rather than re-arguing the rule.

Finally, we review the policy after about a year, with data. How many compile breaks from new variants caught a real missing behaviour, and how many only forced a coordinated release with no logic change? How many incidents came from unknown values at boundaries? If, say, 40 of 50 breaks caught a real gap, the rule earns its cost. If most were noise from one shared library, that library's design is the thing to change, not the rule.

6Follow-up questions

Is MatchException at run time a good enough safety net?

It is a good last resort, because it fails loudly instead of doing the wrong thing. But it fires in production, in whatever code met the value, which is the worst place to learn about a new variant. Converting at the boundary means unknowns are caught where we can log, alert and route them deliberately.

What about switches over enums from other teams?

The same thinking applies. An enum is also a closed set, and a switch expression over it without default breaks when a constant is added. For enums deserialized from external data, the boundary parser should map unknown constants explicitly; Jackson, for example, can be configured to map unknown values to a chosen default constant.

How do you get teams to accept this?

By showing the cost both ways with real examples from our own incidents, keeping the rule short, automating the checks so reviewers do not have to argue, and giving an exception process. Most engineers agree quickly once they see a production bug that a default branch hid.

7Conclusion

In this answer, we saw that exhaustive switches move errors to compile time at the cost of coupling every switch to the full set of variants. The guidance that balances this is: no default inside a module or team, one explicit conversion with unknown handling at every boundary, sealed hierarchies shared across many teams only with a controlled release cadence and new variants treated as breaking, all recorded in guidelines and enforced with lint rules, and reviewed with data after a year.

What to say in the interviewAbout 90 seconds aloud

Open with

My guidance would depend on where the switch sits. Inside a module or within one team's release unit, I want exhaustive switches with no default, because when someone adds a variant the compiler lists every place that needs a decision, and fixing those in the same change is cheap.

Then, if they want more

At boundaries, where data comes from other teams, partners or old clients, new values can arrive at run time with no compile step, so I would convert incoming data into our internal sealed model in one parser at the edge and handle unknown values there explicitly: map them, wrap them in an Unknown variant, or reject them to a dead-letter queue with an alert.

Everything behind that parser stays exhaustive. The risky case is a sealed hierarchy in a shared library that many teams switch over.

Adding a variant then breaks everyone's build or throws MatchException in services that did not recompile, so I would avoid exposing hierarchies like that unless we control the release cadence, for example in a monorepo, and otherwise treat a new variant as a breaking API change.

I would write this into our API guidelines, enforce it with lint or ArchUnit rules that flag defaults over sealed types in core code, and after a year review how many compile breaks caught real bugs versus how many only forced coordinated releases.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Exhaustiveness checking moves some errors from run time to compile time, but couples...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/7deb719c9c/