Re:Checked exceptions: Java’s biggest mistake (2014)
by chriswarbo on Monday September 14, 2020 @01:39PM
(#24472851)
> Rust developers can fluently switch and convert between conrete `Result<T, SomeErrorEnumeration>` and `Result<T, dyn Error>`
That's something different, since both cases tell you that errors might occur.
In Java we can do the following:
public int concreteChecked() throws FileNotFound {
if (bar) throw new FileNotFound();
return 42;
}
public int polymorphicChecked() throws Exception {
if (bar) throw new FileNotFound();
return 42;
}
public Either<FileNotFound, int> concreteEither() {
return bar? new Left(new FileNotFound()) : new Right(42);
}
public Either<Exception, int> polymorphicEither() {
return bar? new Left(new FileNotFound()) : new Right(42);
}
public int unchecked() {
if (bar) throw new RuntimeException(new FileNotFound());
return 42;
}
I think your 'Result' examples are like the third and fourth examples above: using a sum type, differing by whether the error is more/less specific.The first and second use checked exceptions, again differing in whether the error is more/less specific. Importantly: these will refuse to compile if we don't have 'throws ...' in their signature.
The last example uses an unchecked exception: if we throw 'RuntimeException' (or a subclass), we don't need to put 'throws ...' in the signature, and hence the compiler won't keep tell us to put 'catch' block anywhere.
Re:Checked exceptions: Java’s biggest mistake (2014)
by dpc_pw on Tuesday September 15, 2020 @01:02PM
(#24483593)
In Rust you can do `Result<T, dyn SomeErrorInterface>` as well, which makes it able to express everything exactly like Java can. It's just Rust community generally doesn't bother with the taxonomies of errors. You either get a concrete list, or "any error".
Rust developers can fluently switch and convert between conrete `Result<T, SomeErrorEnumeration>` and `Result<T, dyn Error>`. It works beautifully. Libraries usually enumerate their errors (leafs), applications usually just throw everything to one universal error bag.