Skip to main content

catch_unwind_with_details

Function catch_unwind_with_details 

Source
pub fn catch_unwind_with_details<F, R>(f: F) -> Result<R, CaughtPanic>
where F: FnOnce() -> R + UnwindSafe,
Available on crate feature panic and non-target_family=wasm only.
Expand description

Like catch_unwind_str, but on panic also recovers the panic’s source location and a backtrace captured at the panic site, bundled together in a CaughtPanic.

The standard catch-unwind machinery only recovers the panic payload (the message); the location and backtrace are otherwise only available inside the panic handler, which runs before the stack is unwound. This function opts in to having the enhanced panic handler (see install_enhanced_handler) stash those details so they can be attached to the returned error.

Capturing a backtrace is relatively expensive, but the cost is only paid when a panic actually occurs, so this is suitable for enriching internal errors with extra context. If install_enhanced_handler has not been installed, the location and backtrace fields will be absent, but the message is still recovered.

Note that the captured backtrace is always a full backtrace (Backtrace::force_capture); unlike the aborting path in install_enhanced_handler, it does not honor RUST_BACKTRACE, since these caught panics are rare and the extra context is worth the verbosity.