4 ms·
In my opinion, exceptions should be for really exceptional situations - things you can't realistically expect and/or which just signal a programming error (e.g.
by Tainnor 2y ago
In my opinion, exceptions should be for really exceptional situations - things you can't realistically expect and/or which just signal a programming error (e.g. a precondition being violated). They aren't usually recoverable locally and should bubble up the stack, so you can handle them at some boundary (e.g. so you can serve up a 500 page to a request without crashing the entire server for everyone).
For things that are expected to go wrong sometimes, algebraic data types (including Result types) are better. They are part of the type system, so everything you can do with types, you can do with them, including making functions parametric over whether they return an "error" or not. Exceptions need extra mechanisms so that functions can be parametric over them (e.g. Swift has "rethrows", Java has... nothing). They aren't really composable with anything else.