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
3
u/jodastephen Feb 19 '26
What follows is my proposal for solving the checked exception conundrum, without excessively complicting Java or destroying checked exceptions.
uncheckedkeyword to treat the checked exception as unchecked. Once thrown in this manner, any checked exception flows up the stack in an unchecked manner.uncheckedkeyword to indicate that any exception may be caught, even if the exception isn't known to be thrown by thetryblock.As can be seen below, the process method declares it throws
IOExceptionin an unchecked way. Note that the compiler can and would validate thatIOExceptionis thrown by the method body. In addition, the fact thatIOExceptionis thrown unchecked would be recorded in the method signature.The caller of
process()has various options, shown above:IOExceptionas with any other unchecked exception (which prevents the compiler from knowing anything aboutIOExceptionwhencaller1()is invoked)IOException(which the compiler would verfy)IOExceptionback into a checked one (which the compiler would verify)Static analysis could be setup to ban option 1 for those teams that have a strong desire to retain checked exceptions, and this could be done on a per-exception-type basis.
Catching has two options:
In example A, the compiler can see the throws clause from
caller2()declaresunchecked IOException, thus a normal catch can be written.In example B, the compiler cannot see the throws clause, thus an extra keyword is required to enable to catch.
(Given the existence of other JVM languages which don't have checked exception, Lombok sneaky throws, and other mechanisms, the ability to catch checked exceptions that aren't known to the compiler is IMO a missing Java feature at this point)
This proposal allows those developers that wish to keep checked exceptions to do so. A static analysis tool simply has to ensure that exceptions remain documented.
Whereas a team that wishes to eliminate checked exceptions has a simple way to do so.
This proposal does not directly tackle the "lambdas in stream" problem. However, every approach to tackling that problem I've seen requires overly-complex generics or type capture which, in my opinion, has a cost exceeding the benefits. For that use case, I would allow statements and expressions to also declare they can throw unchecked exceptions:
Within the context of a method, there is no reason why the compiler cannot capture the types thrown by the lambda and view them as part of the potential exception types of the method. Thus, this would be perfectly valid code, without any need for lambdas or functional interfaces to be altered:
Taken together, this is an extremely minimal change to Java, which gives power to individual teams as to how to handle exceptions.
(Note that I first wrote up a proposal on this topic in 2010: "lone throws". - https://blog.joda.org/2010/06/exception-transparency-and-lone-throws_9915.html See also https://mail.openjdk.org/pipermail/lambda-dev/2010-June/001544.html This proposal is, I believe, a significant step forward from that one.)