News for hackers. Stuff that matters.
Re:The Reason Java Is Still Popular by kaba0 on Thursday September 22, 2022 @04:41PM (#32944346)
Checked exceptions are an exact analog for result types. If you have an `int parseInt(String s) throws ParseException` function it is basically equivalent to `parseInt(s: String): Result<Integer, ParseError>`. Hell, if anything, it is better — it is automatically “unboxed” for you, and in case of an error you get the sane default choice of bubbling said exception up (rust’s ? macro does the same, but it is much less ergonomic). It has the added benefit of including stack traces, which makes it actually possible to do anything with unhandled error cases and is of utmost importance.

I’m not claiming that Java’s execution of checked exceptions is good, it unfortunately does have a few inconsistencies (lambdas don’t operate well with them, also inheritance is not a good match for exceptions), but it was pretty universally ditched and I think it very well deserves another chance.

Re:The Reason Java Is Still Popular by piva00 on Thursday September 22, 2022 @06:51PM (#32945643)
My issue with checked exceptions in Java is that it becomes another branch of control flow. It increases cognitive load to keep in mind that exceptional cases are basically running a `GOTO` that you have to keep track of.

I think they can have its place but operating well with lambdas would be a must... If Java supported a multi-value return, like Go and Python do, and control flow was based on `var a, err = obj.exec()` or some other syntax that could work nicely with Streams and the functional features of Java I wouldn't have an issue with checked exceptions. A multi-return for errors with checked exceptions would even allow the compiler to narrow down all the possible `err` types you could get, automatically generate a Java 17 `switch()` to parse through all the possible cases of checked exceptions. It could help a lot with error handling.