Description
Reading the standard library documentation for panic
, one might think that catch_unwind
(https://doc.rust-lang.org/std/panic/fn.catch_unwind.html) would take precedence over any registered panic hook, i.e., if you used both catch_unwind
and set_hook
in a program, any unwinding panic thrown in the catch_unwind
scope should be caught. But, this doesn't appear to be the case. See this simple example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a0167a26203b23f5b50efcd1478b54d7
fn bar() {
println!("bar() is called!");
}
fn foo() {
let res = std::panic::catch_unwind(|| {
bar();
panic!("Panicking after calling bar()!");
});
println!("Unwind closure returned: {:?}", res);
}
fn main() {
std::panic::set_hook(Box::new(move |_| {
println!("Panic handler invoked!");
std::process::exit(12);
}));
foo();
}
I'd expect Unwind closure returned: Err(Any { .. })
to be printed, but it doesn't get printed. Instead, the panic handler is invoked.
Can you elaborate if this is the correct behaviour/intent? And if so, why? It seems to me that it would be useful to support both concurrently, i.e., allow the catch to handle the panic, and if it cannot, it could fallback to the registered handler?