3 ms·
> runtime exceptions are not the solution They are part of the solution. Good programming needs to handle errors. Monadic error-handling gets you most of the
by mrkeen 2y ago
> runtime exceptions are not the solution
They are part of the solution.
Good programming needs to handle errors. Monadic error-handling gets you most of the way, combining the local reasoning of return values with the terseness of unchecked exceptions.
But there are simply too many places where something can go wrong to try to enforce deliberate error-handling everywhere. TFA mentioned indexing into arrays. Integer operations resulting in overflow is another example. To stay sane these fails just have to come out exceptionally.
People writing about Java got it right for a long time: "Exceptions should be used exceptionally". But just like "null is the billion dollar mistake" it hasn't made it into the way people program yet.
- bennettnate5 2y ago> But there are too many places where something can go wrong to try and enforce deliberate error-handling everywhere Rust actually defines APIs to do just that. Integer overflows can be handled with the `checked_add` family of functions. Indexing can be handled with `get` and `get_mut` on slices. Some choose to develop Rust libraries/projects using only APIs that explicitly cannot panic. It's certainly not for everyone, but I find it incredible that Rust allows that option to develop code that is not only safe, but also robust against crashing the process (1). 1 - barring compiler/standard library bugs and OOM, which more so depends on the runtime
- Ingaz 2y ago"Exceptions should be used exceptionally" - I think golang `panic-recover` does exactly this. They made using `panic` intentionally hard. Motto: "Don't panic in your code"