3 ms·
> I unfortunately know very little about Rust, but is it possible to recover from panics in Rust? Yes: https://doc.rust-lang.org/std/panic/fn.catch_unwind.html
by burntsushi 7y ago
> I unfortunately know very little about Rust, but is it possible to recover from panics in Rust?
Yes: https://doc.rust-lang.org/std/panic/fn.catch_unwind.html https://doc.rust-lang.org/std/panic/fn.catch_unwind.html
However, this comes with a critical caveat: unwinding can be disabled when compiling an application, which means one cannot guarantee that catch_unwind will actually work. (If unwinding is disabled, then panics turn into unrecoverable aborts.)
For this reason, and because the standard error handling mechanism is done through return values, panicking is not an acceptable way to do robust error handling in Rust. Recovering from panics is useful in niche scenarios, like keeping a web server running even if a request causes a panic or in tests for ensuring that all tests run even if one panics. Which basically matches your key concern here.
(To be clear, I think this is mostly orthogonal to my original comment in this thread. :-))
- Tainnor 7y agoYep, that sounds like kind of what I would need. :) But in general, exception handling is hard. There is value in having locally-unrecoverable, but globally-recoverable faults, stuff like that is probably where languages like Erlang shine.