Skip to main content

catch_user_panic

Function catch_user_panic 

Source
pub fn catch_user_panic<F: FnOnce()>(site: &str, f: F)
Expand description

Run f and swallow any panic it produces.

On panic, writes a best-effort diagnostic to stderr identifying the callback site and the panic message (when the payload is a &str or String). Diagnostics and panic-payload destruction are contained by an outer unwind boundary. A panic payload whose destruction produces one panic is contained; multiple panicking destructors within one payload aggregate can still abort as required by Rust’s double-panic semantics.

AssertUnwindSafe is required because trait objects are not generally UnwindSafe and we accept the user’s responsibility for their own state consistency on panic.

§Panicking capture destructors

This function cannot recover if f panics and destruction of one of its captures also panics during that unwind; Rust aborts on that double panic. Keep potentially panicking teardown state out of f and use catch_user_panic_result_with_cleanup instead.

f, its captures, body-local values, and its panic payload must not produce multiple destructor panics during one aggregate destruction.