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".
That's something different, since both cases tell you that errors might occur.
In Java we can do the following:
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.