4 ms·
> However in Rust, panics cannot be recovered from, there is no way to incept a panic in the current thread. You definitely can: https://doc.rust-lang.org/std/
by Diggsey 3y ago
> However in Rust, panics cannot be recovered from, there is no way to incept a panic in the current thread.
You definitely can: https://doc.rust-lang.org/std/panic/fn.catch_unwind.html https://doc.rust-lang.org/std/panic/fn.catch_unwind.html
- RichieAHB 3y agoThis is true for unwinding but from the Notes section on the linked page, panics can’t be caught using that function if the panic aborts the process.
- loeg 3y agoAs sibling alludes to, there is no guarantee that panic unwinds the stack. It can just abort immediately.
- burntsushi 3y agoYes, that's what the docs linked say. And the GP is correct in saying "you definitely can." The GP is a correction to the article which incorrectly states that "there is no way to incept [sic] a panic in the current thread." The correct phrasing would be "there is no way to guarantee that you can recover from a panic in the current thread." In the context of the article, this is a somewhat minor point. Because you can't guarantee the recovery from panics, you often code as if you can never recover from panics. And thus, when you write code that can panic (for example, `slice[i]`), it's best to conservatively assume that any such panic won't be recovered from. Still though, the existence of a recovery mechanism can be useful advice in some circumstances: https://docs.rs/regex/1.*/regex/#panics https://docs.rs/regex/1.*/regex/#panics The broader story here is a knot that is difficult to untangle because 1) people have different values and 2) words mean different things to different people. I tried to untangle some part of it here (although it's more broad than just "panics can or can't be recovered from," it's still all connected): https://blog.burntsushi.net/unwrap/ https://blog.burntsushi.net/unwrap/
- Groxx 3y agoTo extend on this: application developers can actually guarantee it, because they control the compilation process, which is what decides if it aborts or not. Libraries have less control / no guarantee.
- burntsushi 3y agoThat's right, but it doesn't give you a guarantee in every sense of the word. A panic during unwinding will still result in an abort.
- littlestymaar 3y ago> To extend on this: application developers can actually guarantee it, because they control the compilation process, which is what decides if it aborts or not. They can control that panic isn't compiled as abort, but even then, if there's a destructor that can panic, then if it panics during unwinding, it aborts. So even the app developer cannot guarantee to catch all panics.