Function panik::run_and_handle_panics[][src]

pub fn run_and_handle_panics<R: Debug>(
    do_me: impl FnOnce() -> R + UnwindSafe
) -> Option<R>

Runs the given closure, catching any panics that occur on all threads while in the scope of the closure.

See Builder for configuration.

The Debug bound on R is only for logging purposes (in the event a successful result is swallowed by a panic on another thread) - see run_and_handle_panics_no_debug for an unconstrained return value.

This function can be called multiple times serially, but cannot be nested.

Return value

If any thread(s) panicked, None is returned and the offending Panics are available in panics and has_panicked until the next call to this function. Otherwise if no panics occur, Some(R) is returned.

Example

See the tests/ directory for more examples.

let result = panik::run_and_handle_panics(|| panic!("oh no"));
assert!(result.is_none());
assert!(panik::has_panicked());

let panics = panik::panics();
assert_eq!(panics.len(), 1);

let panic = &panics[0];
assert_eq!(panic.thread_id(), std::thread::current().id());
assert_eq!(panic.message(), "oh no");