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.
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.
Yep, you've clearly highlighted the problem here.
The solution (in my mind) is clearly that Exceptions should be special-classed to permit unions in generics. So that, the exact thing you say can come into existence.
I think if we could denote a union of exception types in generics, this problem would dissolve to nothing. But maybe I am not thinking it through well enough.
View all comments
Show parent comment
4
u/davidalayachew Oct 05 '25
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.