r/java • u/javaprof • Aug 27 '25
Community JEP: Explicit Results (recoverable errors)
Java today leaves us with three main tools for error handling:
- Exceptions → great for non-local/unrecoverable issues (frameworks, invariants).
- null / sentinels → terse, but ambiguous and unsafe in chains/collections.
- Wrappers (Optional, Either, Try, Result) → expressive but verbose and don’t mesh with Java’s switch / flow typing.
I’d like to discuss a new idea: Explicit Results.
A function’s return type directly encodes its possible success value + recoverable errors.
Syntax idea
Introduce a new error kind of type and use in unions:
error record NotFound()
error record PermissionDenied(String reason)
User | NotFound | PermissionDenied loadUser(String id);- Exactly one value type + N error tags.
- Error tags are value-like and live under a disjoint root (ErrorTag, name TBD).
- Exceptions remain for non-local/unrecoverable problems.
Examples
Exhaustive handling
switch (loadUser("42")) {
case User u -> greet(u);
case NotFound _ -> log("no user");
case PermissionDenied _ -> log("denied");
}Propagation (short-circuit if error)
Order | NotFound | PermissionDenied | AddressMissing place(String id) {
var u = try loadUser(id); // auto-return error if NotFound/PermissionDenied
var a = try loadAddress(u.id());
return createOrder(u, a);
}Streams interop
Stream<User | NotFound> results = ids.stream().map(this::loadUser);
// keep only successful users
Stream<User> okUsers = results.flatMap(r ->
switch (r) {
case User u -> Stream.of(u);
default -> Stream.of();
}
);
9
Upvotes
View all comments
Show parent comment
3
u/vips7L Aug 28 '25
In theory, checked exceptions do work with lambdas. Scala and Swift have both proven that with a sufficiently advanced type system it’s possible, it’s just up to the OpenJdk team to make that work.
The larger issue is the culture imo. To this day you’ll find old timers with advice like “exceptions should be exceptional” or that no one handles exceptions anyway and they just let them bubble up to a top level catch or like you said the people that will sneaky throw them into oblivion.
Personally, I expect without a culture shift and investment into error handling in the language I’m just going to go elsewhere for my choice of language where they care about correctness. Though I don’t know what that is. Scala? Kotlin? Swift? The main things I want in a language are checked nulls, checked errors, and a GC; native compilation would be a bonus. Most of the issues or bugs I face at work are all from people not codifying the rules into the type system.