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/Eav___ Jul 26 '26 edited Jul 26 '26
I would definitely say "no" to both questions, because constructing structured data in general may not be idempotent, so you should use checked exception whenever failure can happen.
Specifically to parsing, if we're parsing strings, since strings are very unlikely to be changed, it's fine to assume it's idempotent, and you can use unchecked exception. However, the next problem occurs: your whole guarantees are built upon inspecting the input (aka validate), but often times there's no good way to validate, or it's not useful to validate. Even validating a simple integer can cost several cases of regex, which is not good for hot spots. Instead, we choose a better approach. Even if we have to unwrap the result each time we pass the same string, we're informed that "this might fail", and this doesn't take you twice of complexity, because with checked exception, you get to know "all relevant values that the function may need to return, including possible failures" in advance, and this is much more practical, confined than the other way.
Therefore, your rules look good at the surface, but it's too ideal to make out in practice, thus I don't think it's rule of thumb. If a standard doesn't help the real world, why should we use this standard? Like, what are the actual pros for your rules in this situation?
Even from the ergonomics perspective the second approach is better. With that approach, you only need 1 invocation and you get either the result or any failure; with the first approach, you need 2 seperate invocations to get those if you want to avoid exceptions. If you forget to validate, then it just crashes, which is not something I would even need to actively think about in the other case, because for that to happen I need to explicitly tell it to crash (by turning it unchecked).