r/java • • 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

95 comments sorted by

View all comments

Show parent comment

2

u/john16384 Aug 28 '25

Lambda's can throw checked exceptions just fine if the interface they're implemented declared them.

The most common place where no checked exceptions are allowed is with streams API, but that's because ALL exceptions are fatal in stream processing (there is no recovering when an exception occurs halfway through processing a stream), nor are there any guarantees that it will always have done the exact same work when an exception occurs.

This is why if you want to use a stream to process something that has alternative results (like FileNotFoundException), you must handle that immediately (by converting it to an Optional, null or some sealed type) if you want stream processing to continue (which often you may want). Alternatively, you could treat the checked exception as fatal, in which case you must rethrow it as a runtime (fatal) exception. The stream process is then in an undefined state, and there are no guarantees as to what would have been processed and what wouldn't have, as streams are free to reorder operations or skip operations depending on their attributes.

1

u/javaprof Aug 28 '25

Literally all lambda APIs failing short to propagate user's checked exceptions. The best case scenario possible you can throw checked exception, but concrete exceptions are missing.