Implicit stack-unwinding exceptions are terrible
Implicit stack-unwinding exceptions are terrible
Posted Sep 22, 2024 9:49 UTC (Sun) by summentier (subscriber, #100638)In reply to: Exceptions? by zorro
Parent article: Best practices for error handling in kernel Rust
1. Exception safety together with RAII seems simple but is actually fiendishly difficult to get right – Stroustup's paper on execption safety in something as basic as an array has 17 pages, auxiliary base types, etc.
2. Add parallelism to the mix, and you have to reintroduce a monad (`Result`, `std::future`) anyway. Oh, by the way, are you sure your network of threads is notified and unwound in the correct order by the default behaviour?
3. Are you sure the calling code knows which exceptions to expect? Are you sure you know which ones you are passing along without even knowing from third-party libraries? Are you sure none of these libraries are adding or changing expections in new versions? The function signature is certainly of no help to you there...
4. As the C++ FAQ points out, exceptions allow you to factor out code to helper functions. Sure, but are the exceptions the helper raises still relevant to the caller of the upper-level function? Is, e.g., "log2 expects positive argument" something you want to expose from a config file parser rather than "Invalid entry: expect n to be positive"? Something you want to guarantee and maintain?
In an ideal world, I would like to have a "decay" mechanism, where Results, when assigned to a variable that is not Result, are unpacked and a panic raised if that fails, just to free up some mental space of new programmers or computational scientists who do not want to think about error paths all the time. Other than that, I think Rust gets this right.