Skip to main content

catch_panic

Function catch_panic 

Source
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

§Returns

  • Ok(result) - with body1’s return value if it completes without panic
  • Err(panic) - if body panics in process, with Panic as error value