News for hackers. Stuff that matters.
Re:Checked exceptions: Java’s biggest mistake (2014) by hota_mazi on Friday September 11, 2020 @02:46PM (#24446032)
Right.

They don't just "encourage" developers to consider error paths, they "force" them to do so.

The concept of checked exceptions is very sound, as is the more general concept of compiler enforced error checking. Very, very few languages have that (only Java and Kotlin in the mainstream league).

Languages with a solid implementation of algebraic data types offer a good first step in that direction but they still require users to manually bubble and compose monadic values, which introduces an unnecessary, and sometimes intractable, level of obfuscation and boiler plate.

All other languages provide weaker approaches to this concept that are library enforced, not language enforced, and therefore more prone to being overlooked since they require discipline from the developer.

Re:Checked exceptions: Java’s biggest mistake (2014) by dllthomas on Friday September 11, 2020 @08:45PM (#24448865)
I conjecture that much of the legitimate pain from checked exceptions in Java were because the language to talk about exceptions was so limited.

For instance, consider fold or map functions. There was no way of saying "this might throw anything that might throw", so the only option was "this won't throw anything and that cannot throw anything" or "this might throw anything".

Without the necessary flexibility, developers aren't "forced" to consider only error paths but also manifold impossible paths that aren't easily distinguished from legitimate error paths.

Re:Checked exceptions: Java’s biggest mistake (2014) by ragnese on Friday September 11, 2020 @05:44PM (#24447661)
Kotlin has no mechanism to force error handling. You may suggest that we use sealed classes as return values and match on them, which is okay-ish, but suffers from two big issues:

1. You are not forced to use the result, so if you call a function for its side effects, the compiler will not warn you that you should unwrap the value. Rust and Swift do not have this issue.

2. This is extremely tedious, as you mention. But it's specific to Kotlin and not generally true as you later suggest. Haskell had monad comprehension (do notation), Scala has the same thing, Rust has the ? operator.

Also, regarding Kotlin; no library, nor the standard library, does anything other than throw unchecked exceptions on all kinds of failures. Kotlin is a step backwards, IMO.

Also PHP has checked exceptions, like Java.

Re:Checked exceptions: Java’s biggest mistake (2014) by Groxx on Friday September 11, 2020 @06:56PM (#24448172)
2 specifically, just to um-actually a minor detail: Rust does have this problem. This code: https://repl.it/repls/LoathsomeCompleteColdfusion

    #[must_use]
    fn get() -> i32 { 
      5
    }

    fn main() {
      get();
    }
will compile and run. It does emit a warning by default at least though:

    warning: unused return value of `get` that must be used
     --> main.rs:7:3
      |
    7 |   get();
      |   ^^^^^^
      |
      = note: `#[warn(unused_must_use)]` on by default
Without the `#[must_use]`, you won't even get that warning. Result has it, so it's common, but it's definitely not enforced. To get the Result's value though, yes - there's no way around it.
Re:Checked exceptions: Java’s biggest mistake (2014) by ragnese on Friday September 11, 2020 @08:07PM (#24448659)
You said you were picking on my second point, but you actually addressed the first.

I was specifically talking about Result and Option, which, as you mentioned, are marked must_use. It's correct that it's a warning, but I think that's good enough. You won't accidentally forget to use it.

Re:Checked exceptions: Java’s biggest mistake (2014) by hota_mazi on Friday September 11, 2020 @05:56PM (#24447762)
> Kotlin has no mechanism to force error handling.

I was referring to nullability support, which is similar to Option/Maybe, except that it's enforced by the compiler. This is pretty unique to Kotlin at the moment (honorable mention to Ceylon which offered similar support).

Re:Checked exceptions: Java’s biggest mistake (2014) by iainmerrick on Friday September 11, 2020 @07:27PM (#24448400)
Unique to Kotlin? Don’t Swift and TypeScript have this feature too?

Maybe it’s just the languages I’ve been working with this recently, but I think of this as something that’s breaking into the mainstream as language designers are starting to agree that it’s a good idea.

Re:Checked exceptions: Java’s biggest mistake (2014) by dragonwriter on Friday September 11, 2020 @10:03PM (#24449228)
> I was referring to nullability support, which is similar to Option/Maybe, except that it's enforced by the compiler.

Option/Maybe is enforced by the compiler on most languages that have it. Nullability as part of type signatures differs not because it is compiler-enforced, but because it is not nestable; it provides no equivalent of Maybe[Maybe[T]].

> This is pretty unique to Kotlin at the moment.

C# 8, Python’s typing module, Java 8 via the Nullness Checker, Sorbet for Ruby, and lots of other statically-typed languages or static-analysis packages for languages (even dynamically-typed languages) provide nullability enforcement. It's definitely not unique to Kotlin.

Re:Checked exceptions: Java’s biggest mistake (2014) by hota_mazi on Friday September 11, 2020 @10:30PM (#24449332)
> Option/Maybe is enforced by the compiler on most languages that have it.

It's enforced by exactly zero languages.

It's up to the developer to decide that their function should return an Option/Maybe instead of the naked value.

By definition, any library construct is NOT enforced by the compiler, because... well, it's a library construct and not a language construct.

Re:Checked exceptions: Java’s biggest mistake (2014) by dragonwriter on Friday September 11, 2020 @10:33PM (#24449345)
> It's enforced by exactly zero languages.

It's enforced in exactly the same way a nullability constraint is; if you don't use Maybe[T] instead of T , you can't use None (presuming T itself isn't Maybe[U]), just as if you don't use a nullable type you can't use null.

Re:Checked exceptions: Java’s biggest mistake (2014) by ragnese on Friday September 11, 2020 @08:08PM (#24448674)
I like Kotlin's null most of the time, but I actually prefer Option still because every once in a while I need to nest them. You can nest Option, but not null.
Re:Checked exceptions: Java’s biggest mistake (2014) by valenterry on Friday September 11, 2020 @09:51PM (#24449181)
I think you should look into some more programming languages before you claim that this is unique to Kotlin. ;)
Re:Checked exceptions: Java’s biggest mistake (2014) by augusto-moura on Friday September 11, 2020 @03:13PM (#24446294)
You are not forced per se, and can have the same level of obsfucation. It's pretty common to find code ignoring exceptions or wrapping them in RuntimeExceptions, like:

  try {} catch (Exception e) {
    throw new RuntimeException(e);
  }
Also not doing nothing, or the famous catch and log
Re:Checked exceptions: Java’s biggest mistake (2014) by zmmmmm on Friday September 11, 2020 @05:29PM (#24447508)
At which point it's super easy to identify in code review and slap the developer on the wrist.

I find these arguments that posit incompetent / ignorant developers as a hurdle a bit strange. If they are going to incompetently handle errors wrong when explicitly forced to handle them I can't even imagine how poor their code will be without any assistance from the compiler, and it seems awful to think that you will have no way to identify such poor handling on review - you're going to have to look up every function they call and check if it can return an error or not manually.

Re:Checked exceptions: Java’s biggest mistake (2014) by Groxx on Friday September 11, 2020 @06:43PM (#24448086)
i.e. Go. Go can make sure you are aware that an error exists (often. linters do pretty good here too), but it does next to nothing to help you handle it correctly.

From personal experience: yes, little to no compiler help on errors takes an enormous amount of effort by both authors and reviewers (and future readers) to ensure correct handling. The vast majority of the time it's just `if err != nil { return err }`, which is very frequently sub-optimal. But without knowing the call in complete detail, you can't judge if that's true or not... and it may have changed since you last saw it.

IDEs help that kind of "is this optimal/correct" question quite a lot, but they can't verify it either. It's question-marks all the way down, unless you fully know all the code you call, which is often infeasible.

Re:Checked exceptions: Java’s biggest mistake (2014) by quantified on Friday September 11, 2020 @10:29PM (#24449331)
The dev may need to. If you are implementing an interface method and the signature does not include the exception, it must be wrapped in the implementation. You’d like this not to be the case, but it’s better than trying to rewrite major dependencies. I hope they can find a more specific RuntimeException subclass to throw, but that’s a relatively minor quibble.

I’d like to suggest a different POV for your comment on code review. Every method can throw exceptions, that’s life with the JVM. You don’t declare IllegalArgumentException, ArrayOutOfBoundsException, NoSuchElementException, etc. Yet your code needs to deal with it and usually does nothing because it means the element doesn’t make it into a collection, the rest of the object doesn’t get constructed, etc. Avoiding raw RTE, and instead using an RTE subclass that conveys the information you care about works fine. Code review avoids raw RTE, and all places that might care about IO exception causes etc. can process the caused-by of the wrappers.

In what kind of code has this technique been an actual problem? I agree standards and techniques need to be rigorously applied.

Re:Checked exceptions: Java’s biggest mistake (2014) by staticassertion on Friday September 11, 2020 @03:39PM (#24446521)
You aren't forced to handle it well, you are forced to handle it.
Re:Checked exceptions: Java’s biggest mistake (2014) by ragnese on Friday September 11, 2020 @05:47PM (#24447681)
You aren't forced to write your happy path code well, either. What's your point?
Re:Checked exceptions: Java’s biggest mistake (2014) by dllthomas on Friday September 11, 2020 @08:46PM (#24448873)
The point seemed to be that checked exceptions are a bit of a help, whereas you seem to be responding to an implied claim that they aren't helpful. I think that's why you're being downvoted.
Re:Checked exceptions: Java’s biggest mistake (2014) by ragnese on Friday September 11, 2020 @09:40PM (#24449135)
Perhaps. I definitely read it as them saying checked exceptions aren't helpful because you can still handle the errors poorly. But I can see how I might have misinterpreted the intent.
Re:Checked exceptions: Java’s biggest mistake (2014) by staticassertion on Saturday September 12, 2020 @07:18PM (#24456743)
I suppose I wasn't clear. You did misinterpret what I said, I intended to convey the exact opposite.
Re:Checked exceptions: Java’s biggest mistake (2014) by mcguire on Friday September 11, 2020 @03:56PM (#24446683)
Once upon a time, I worked on a C++ project with another, experienced developer who kept commenting out -Wall from the Makefiles with the comment, "Nobody has time for that."
Re:Checked exceptions: Java’s biggest mistake (2014) by hota_mazi on Friday September 11, 2020 @05:56PM (#24447767)
You are still forced to deal with it.

In your example, the developer just chose to write crappy code, and there's no defense against that.

Re:Checked exceptions: Java’s biggest mistake (2014) by trefil on Friday September 11, 2020 @03:14PM (#24446299)
Not sure if Rust counts as mainstream by your metric, but it does have compiler enforced error handling in the form of the Result sum type.
Re:Checked exceptions: Java’s biggest mistake (2014) by TazeTSchnitzel on Friday September 11, 2020 @06:38PM (#24448060)
> compiler enforced error checking. Very, very few languages have that

  __attribute__ ((warn_unused_result))
in C/C++ with GCC and Clang is great.
Re:Checked exceptions: Java’s biggest mistake (2014) by vinkelhake on Friday September 11, 2020 @10:20PM (#24449297)
And standardized as [[nodiscard]] in C++17.
Re:Checked exceptions: Java’s biggest mistake (2014) by MaxBarraclough on Saturday September 12, 2020 @11:56AM (#24453358)
Re:Checked exceptions: Java’s biggest mistake (2014) by amw-zero on Saturday September 12, 2020 @08:05AM (#24451813)
Avoiding the “boilerplate” of error handling means that you have implicitly coupled code now. Exceptions make local reasoning not possible. I guess you may want that in a small minority of cases, but I don’t understand the benefit they give you overall. It’s just a shortcut and a hack, and I’d rather see explicit control flow 100% of the time.