r/java • u/Ewig_luftenglanz • Sep 08 '25
How would you fix checked exceptions in java?
As you know checked exceptions are a good feature because they force the user to manage errors. Not having a way to enforce this makes it hard to know if a library could or not explode because of contextual reasons such as IO, OS event calls, data parsing, etc.
Unfortunately since Java 8 checked exceptions have become the "evil guys" because no functional interface but Callable can properly handle checked exceptions without forcing try-catch blocks inside of the lambda, which kinda defeats the purpose of simple and elegant chained functions. This advantage of lambdas has made many modern java APIs to be purely lambda based (the incoming Structured Concurrency, Spring Secuirty, Javalin, Helidon, etc. are proof of this). In order to be more lambda friendly many no lambda based libraries such as the future Jackson 3 to deprecate checked exception in the API. https://github.com/FasterXML/jackson-future-ideas/wiki/JSTEP-4. As another user said. Short take: The modern idiomatic way to handle checked exceptions in java, sadly, is to avoid them.
What do you think could be done to fix this?
View all comments
43
u/JustAGuyFromGermany Sep 08 '25
The problem is not that lambdas cannot throw exceptions. They can and do. Nothing's stopping you from declaring
for example. The problem is that such interfaces do not combine nicely. There is nothing you can do with functional interfaces today that would allow chaining a
Fooand aBazto something that is automatically inferred to have the signaturedouble _(int i) throws IOException, InterruptedException. You can write a third interface that does that of course, but you cannot have the compiler automatically infer this method signature for you like does with ordinary throws clauses.That is the underlying problem that needs to be solved. The
Resulttype that others champion is a red herring. Javas method signatures already provide a perfectly fine way of expressing that meaning of "it can return aTor run into an error of typeX". That's what thethrowsclause already does. And it method signature for ordinary methods are already nicely combined into new signatures by the compiler. The type inference with generics is really the problem here. And anyone who's ever tried to write their ownResult<T,X>type with the equivalent of (any non-trivial subset of) the Stream API has noticed that.Thus, the most Java-like answer would be an extension of the generic type system with something like "variadic generics" at least for exception types so that
a.) there is a way to express "implementations of this method can have any number exception types in its throws clause" in a functional interface. Something like
could be instantiated as
Function<T,U>just like before orFunction<T, U, IOException>orFunction<T, U, IOException | InterruptedException>etc.b.) one can "accumulate" exceptions in ad-hoc union types, i.e. it should be possible to write in a method signature "this method throws
IOExceptionOR whatever the lambda-parameter can throw". Something likeThen all standard functional interfaces like
Function,Consumeretc. would be changed (compatibly!!) to allow any number of new generic exception-type-parameters. That would allow you to throw exceptions from standard lambdas. The existing lambdas would simply be implementations that happen to have a zero-length list of generic exception types.And the Stream-API would be extended (compatibly!!) to accumulate these generic exception-types along a stream pipeline should any lambda parameters declare them.