So that's Jackson and AWS who migrated from Checked to Unchecked Exceptions. At least a few others too.
I really hope the OpenJDK team comes up with something to lessen the pain of Checked Exceptions. Most cases where I see Checked Exceptions used, they are obviously the right tool for the job. The right way shouldn't take this much effort to work around, especially for fluent classes, which most libraries seem to be migrating towards.
It won't stop me from using Checked Exceptions -- I'll take the long way because it's the right one. Just bemoaning the level of effort is all.
I think it's down to the lack of special handling for throws-position generics and how this limits composition.
You'd probably need to be able to express the union-type of exceptions, and optionality of some generic arguments (to make backwards compatible type substitution work) - possibly even a new type of generic argument specific to throws positions...
Very much a straw-man...
interface Function<T, R, throws X> {
R apply(T t) throws X;
<V, Y extends Throwable> Function<T, V, throws X | Y> andThen(Function<? super R, ? extends V, throws ? extends Y> after) {
return t -> after.apply(this.apply(t));
}
}
This brings with it a lot of "and now we also need" baggage... For backwards compatibility you now need to be able infer the throws terms, as the empty set of exception types, or this Function can't be a source compatible drop-in replacement to work with things like Stream.map(Function). And that's just one of several places where this bleeds a little complexity.
This could probably have been achieved with less baggage, back in the (pre Java 7/8) period of lambda design (and concepts like this were raised then back alongside the CICE, BGGA, FCM bun-fight that stole most of the air in that conversation space).
The chosen lambda solution is better in many ways to any of those, but it put aside checked exceptions (and I don't recall anyone clearly saying why other than 'complexity' - there was a lot of delivery pressure I expect... my interpretation though, as an outsider). Putting it aside has left us with some fundamental APIs which now use lambdas heavily, working around this limitation with solutions like suppressed exceptions and UncheckedIOException.
While more could be done for the try-catch ceremony too, to me the biggest pain has come from generics in Java still occasionally feeling like a bolt-on.
This should all be taken as personal frustration with one weaker area in Java, not an indictment of the language or platform (and it's easy for me to throw out opinions when I'm not so close to the flames).
The progress Java continues to make, in mostly painless and safe steps forward, and the huge potential of the big works-in-progress, makes me think that Java's position is still somewhat secure for a fair while yet.
I think you showed it best with your Function<T, R, throws X>.
The fact is, Checked Exceptions are just not a first class feature with Java Generics (the same could be said for primitives too).
There are a lot of possible ways to ease the pain of Checked Exceptions, but this would probably be the most seamless way to accomplish it. Plus, it would be the most Java way to do it too.
Also, firmly agreed about the union of exceptions, though that would be weird that we can only do it for exceptions.
The issue, especially with generics and where lambdas would be used, is there are multiple non-related exception types that could ultimately get involved.
Now imagine that loadFromDatabase throws a checked DataBaseException and storeInRedis throws a checked RedisException. What could the function signature at 2 or 3 look like? Ideally you'd want to see something equivalent to Stream func() throws RedisException, DatabaseException but how do you communicate that with the generics system?
And I think that's the crux of the language design issues with checked exceptions and generics.
This is doable, I believe, in a way that's both backward- and forward-compatible, and we've done some experiments (with syntax that's nearly identical to your example, where a suffix throws type parameter could be left empty and inferred as, say, RuntimeException), but we're too busy with other, higher-priority projects right now to focus on that.
That's great to hear as I've tossed the ideas around for over a decade (and brought it up a few times very few years). Great to hear you think that there is a practical expression of this in the language too, as I'm yet to be happy with the ceremony involved in my place-holder syntax.
I'm still firmly in the camp that checked exceptions are the right solution, if they can be used in all the places that matter, with appropriate levels of ceremony.
Too often the tone of the unchecked/checked conversations seems to be about trading correctness for convenience - a terrible choice. But since it removes the 'must communicate modes of failure' between the library provider and library client (since they can intentionally or accidentally elide 'throws'), even with well-intentioned use, I think it will lead to an overall drop in quality for Java-based systems over time if we continue the slide towards everything-unchecked.
I do agree there are many other, rather more visible, works in progress that yield more significant value (and really appreciate your work) - just a shame this has sat so long.
There are no technical limitations. they could create functional interfaces that declare checked exceptions in their contract just as they did with Callable. The only reason they haven't done that it's because they DO NOT WANT to. Doing so would imply to pollute the JDK with dozens of new functional interfaces and to refactor hundred of API to support the new contracts through method overloading. That would also require to improve the compiler to recognize between interfaces with similar contracts.
There are no technical limitations. they could create functional interfaces that declare checked exceptions in their contract just as they did with Callable. the only reason they do not do that it's because they DO NOT WANT to pollute the JDK with them, and all the refactor required in the API to get make use of these new functional interfaces.
Plus, it wouldn't solve the problem. Being forced to write a try-catch when you aren't using functions that actually throw anything would be a worse situation than we have now.
Or at least lambdas should handle gracefully or throw checked exceptions.
I wonder if it's a technical limitation
I don't know the details, so I'm ignorant.
But if we're day-dreaming here, I'd like it if there was some way that we could tell the compiler "trust me, I'll handle this Checked Exception elsewhere!", and then have the compiler check my math to see that I actually did so.
That way, we wouldn't lose any of the benefits of Checked Exceptions, just get to choose where we have to handle them.
Here's my day-dreaming syntax. This way, we lose none of the benefits of Checked Exceptions, but get to handle them at the place that makes the most sense.
This is never going to work. Those map functions may not be called here at all or ever. Remove the forEach and return the stream and have someone else call a terminal method to see what i mean. This can only work if Stream tracks what will be thrown as part of its generics.
Here is an example that does work, even with today's Java:
This wraps streams (so the signature can be changed) and then tracks up to 3 different checked exceptions as part of the signature to be correctly declared as thrown from any terminal method.
Better tools for error handling in the standard library would be nice e.g. Exception.catchable(request).onError(log).onSuccess(resolve) or InputStream.open(path).map(readStreamToObject).onError(throwUnchecked).onSuccess(insert). It's mostly just synthetic sugar, just like with optionals, but it does make code more readable.
It's mostly just synthetic sugar, just like with optionals, but it does make code more readable.
I see your point, but this throws out the baby with the bath water.
I want to make it easier to work with Checked Exceptions without having to hide or wrap them. I want the Checked Exception to propagate up if I don't handle them. Not wrap them so that they are invisible to everyone above. Obviously, sometimes wrapping is the right choice, but making a helper method for that is easy.
How about the combination of Result<T, E>, destructing and improved switch, exhaustiveness checks, better inference and first class union types.
Java is progressing towards many of those but slowly. Kotiin is missing union types but is otherwise there.
Do you need stack traces? Because that's one thing checked exceptions directly have. With Result<T, E> you can pass stack traces too, it's just manual work to do that in the error cases. For exception like short circuiting just use runtime exceptions.
In one of the recent Java Conference Talks (I'll have to look up which one) they talked about integrating catch functionality into switch expressions, as an exception is basically just another kind of return option. Then the brackets after switch would be the equivalent of a try block.
In one of the recent Java Conference Talks (I'll have to look up which one) they talked about integrating catch functionality into switch expressions, as an exception is basically just another kind of return option. Then the brackets after switch would be the equivalent of a try block.
This is an excellent idea, and is probably the best way to minimize the pain. Though, I would much prefer if we could eliminate the pain by maybe doing something like making adding a methods "throw-ness" to generics. Of course, it's imaginary syntax, so I would have to try it out to know for sure. Still seems like the best solution I've seen thus far.
I really hope the OpenJDK team comes up with something to lessen the pain of Checked Exceptions. Most cases where I see Checked Exceptions used, they are obviously the right tool for the job. The right way shouldn't take this much effort to work around, especially for fluent classes, which most libraries seem to be migrating towards.
It won't stop me from using Checked Exceptions -- I'll take the long way because it's the right one. Just bemoaning the level of effort is all.
Are there actually any plans/discussions to address the pain of checked exceptions (especially in lambdas)?
This isn’t good at all, we are now going to find bugs at runtime that would have previously been found at compile time. Checked exceptions require a developer to put some thought into what should happen when something exceptional happens.
Now this isn’t gong to happen and lazy developers are going to cause things to break at runtime that previously wouldn’t.
This is actually bad! Unchecked exceptions are the Devil! Sure if you want to vibe code a bit and just do happy path then yes, checked exceptions are annoying. But if you want to build stable solid software you have to think about errors. Checked exceptions help you do that.
View all comments
198
u/titanium_hydra Oct 05 '25
“Unchecked exceptions: all Jackson exceptions now RuntimeExceptions (unchecked)”
Sweet baby Jesus thank you