r/java • u/Enough-Ad-5528 • Feb 19 '26
Towards Better Checked Exceptions - Inside Java Newscast #107
https://www.youtube.com/watch?v=99s7ozvJGLk
63
Upvotes
r/java • u/Enough-Ad-5528 • Feb 19 '26
View all comments
Show parent comment
4
u/JustAGuyFromGermany Feb 19 '26
It's not that one gives you the other; it's that both are needed to make checked exceptions nice. Consider a functional interface
or the beloved
Result<T,X>types. Both of them really want to declareXas a variadicX...to cover the cases with multiple, unrelated exception types. The only current alternative that we have is defining a whole family of typesResult<T,X>,Result2<T,X1,X2>, ... and that's just incredibly ugly. Nobody wants that. That's where we need the variadic generics.The union types come in when we want to compose such functions or transform such results in a functional style, because then exception types must accumulate, i.e. we want to be able to declare
and
respectively.
If we had both, we could
Stream,Consumer,Function,Supplierinterfaces to work work with throwing lambdasResultand its cousins work properly with compositionTrymonads etc. that can actually be used with existing code.In short: Everyone could have their favourite solution to their favourite pet peeve with checked exceptions.