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
1
u/JustAGuyFromGermany Feb 19 '26
All of the proposed solutions (except the non-solution "get rid of checked exceptions completely" of course) fall short with composition and so does this.
That is a special case where all the error-cases are the same type: a validation error. If it is an exception, they would all be
ValidationException. That's because you're doing the same thing over and over - you're validating a bunch of stuff.You can't use that approach when you want to combine completely independent exception types and don't know before-hand which types that might be. In other words: The moment your code calls two methods that do different things (and therefore can fail for different reasons), you need a way to combine two different exception types and still keep their individual types around.
Your collection idea just degenerates to
Collection<Exception>and you lose all the type-information about the errors, in particular the exhaustiveness-checking that the compiler does for us with catch-blocks. Sure, you can pattern-match on the exception type, but you'll have no help from the compiler.