pub fn catch_panic<R>(body: impl FnOnce() -> R + UnwindSafe) -> Result<R, Panic>Expand description
Runs provided closure and captures panic if one happens, along with its backtrace
§Limitations
Stable API doesn’t allow to detect from panic hook that panic being handled will unwind. So if you get unwindable panic, panic info will be swallowed. This will be fixed when #92988 stabilizes. Correct API is used on nightly builds, which resolves issue.
§Panic modes
In case crate is compiled in panic mode other than panic=unwind, hook will unconditionally
forward execution to previously installed hook. In case of panic there will be no unwind,
so client code will not get panic info anyway, although we set hook anyway
in case there are nested scoped hooks, to preserve local behavior.
§Parameters
body- closure which should be run with capturing panics. The closure has same requirements as the parameter tostd::panic::catch_unwind. In particular, if you want to assert that closure is unwind safe when type system can’t deduce it, you can usestd::panic::AssertUnwindSafe. Seestd::panic::catch_unwindfor details.
§Returns
Ok(result)- withbody1’s return value if it completes without panicErr(panic)- ifbodypanics in process, withPanicas error value