Skip to main content

catch_unwind

Function catch_unwind 

Source
pub fn catch_unwind<F, R>(f: F) -> Result<R, PanicError>
where F: FnOnce() -> R + UnwindSafe,
Expand description

Invokes a closure, capturing and returning the cause of an unwinding panic if one occurs.

§Thread safety

This is implemented by installing a custom panic hook. This panic hook is a global resource. The hook that we install captures panic info in a thread-safe manner, and also ensures that any threads that are not currently using this catch_unwind wrapper still use the previous hook (typically the default hook, which prints out panic information to stderr).

We assume that there is nothing else running in this process that needs to install a competing panic hook. We are careful to install our custom hook only once, and we do not ever restore the previous hook (since you can always retain the previous hook’s behavior by not calling this wrapper).