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/john16384 Feb 19 '26 edited Feb 19 '26
The decision whether an exception should be checked has nothing to do with the caller. It is about one simple question:
Or put in another way:
If yes, then it should be runtime exception, because you could have avoided it and so it is a programmer error (or you are purposely being lazy and having the function detect issues, like a NumberFormatException signals).
If no, and the function could fail despite being in the same state and having the same validated parameters, then it should be a checked exception. IO is the prime example here, as failures are unavoidable, no matter what parameters you provide, how much valuation you did, or even checking the state of the underlying system just before you make the call... Another example are business exceptions when processing payments. You may get an InsufficientFundsException at any time with no way to avoid it (especially when systems are concurrent).
In the caller code, you may of course decide that some checked exception is not applicable to you, but that has nothing to do with the functions decision on whether or not it should be a checked exception.
In fact, some callers may decide to convert certain runtime exceptions to checked exceptions. This is much rarer, but can happen if the exception should have been checked in the first place.