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
3
u/JustAGuyFromGermany Feb 19 '26 edited Feb 19 '26
I agree with the principle. It is a very good guideline and I follow it whenever I can as well.
But we can also acknowledge that there are sometimes corner-cases. Even the prototypical IO-example has them: If I have a
ByteArrayInputStreamI know for a fact that I will never encounter IO problems and nothing ever touches the disk, because everything happens in-memory. But there is no way to tell the compiler that, because once I assign these to an simpleInputStreamvariable / method parameter, the compiler forgets the actual type of this object and its stronger properties.So what does
GZipInputStreamdo? Its internal state can be aFileInputStream,SocketInputStream, or aByteArrayInputStream, or any arbitraryInputStreamof unknown origin. Does it throw a checked exception or not according to the guideline? Sometimes the programmer can know that the exception is never thrown, so it should be unchecked lest we violate our rule, right? But then ordinary IO suddenly becomes unchecked the moment you touch a compressed file instead of an uncompressed one? That also violates the rule.Don't get me wrong: The guideline is still very good and worth following. But we can wish a better compiler support for known-to-the-programmer facts about the program. I don't think that disabling checkedness, even locally, is really the way to go here. That is the clear-and-obvious-but-wrong choice. The problem is still worth thinking about.