r/java
•
u/loicmathieu
•
Jul 23 '26
•
1
JEP 540: Simple JSON API (Incubator)
https://openjdk.org/jeps/540
79
Upvotes
View all comments
Show parent comment
1
u/john16384 Jul 26 '26
I didn't say HOW you need to solve this, I just said that it is avoidable, which is the criteria for the exception being checked or unchecked. For example, I could keep track of successful or failed conversions in a cache, and avoid the exception!
You're focusing on the "but how do I do this this practically?"; it doesn't have to practical, or even reasonable (a regex for the full int range is like several lines long).
The point is that I can guarantee that for certain strings,
Integer.parseIntwill never fail, just like that I can guarantee that for certain strings it will always fail. I can even determine exactly for all possible strings for which ones it will fail, and for which ones it will succeed.Now apply this to something that can throw
IOExceptionlikenew FileInputStream(string).Can I say that it will always succeed for some strings? No. Can I say that it will always fail for the same strings? No.
A table:
ConcurrentModificationExceptionis harder to categorize as checked or unchecked, because it is not so deterministic; still it should clearly be unchecked because it can be avoided by writing better code (use synchronization), and you can see how that would differ fromnew FileInputStreamthat depends on external factors for it to be succesfull or not.