This code:
fn main() {
foo();
}
fn foo() {
let _x = Foo;
unsafe { bar() }
}
// note: the `unsafe` allows replacing this with
// an `extern "C unwind"` below:
unsafe fn bar() { panic!("bar"); }
struct Foo;
impl Drop for Foo {
fn drop(&mut self) {
panic!("Foo");
}
}
is guaranteed to panic twice, once printing "bar" and once again printing "Foo", and then, the program aborts due to the double panic printing "thread panicked while panicking. aborting".
I wonder what would happen if we replace bar above with an extern "C unwind" { fn bar(); } function implemented in C++ as extern "C" void bar() { throw "bar"; } or similar (e.g. throwing a std::string("bar"), std::runtime_exception("bar"), etc.).
- How would we detect a double panic?
- What error message would be printed?
This code:
is guaranteed to panic twice, once printing
"bar"and once again printing"Foo", and then, the program aborts due to the double panic printing"thread panicked while panicking. aborting".I wonder what would happen if we replace
barabove with anextern "C unwind" { fn bar(); }function implemented in C++ asextern "C" void bar() { throw "bar"; }or similar (e.g. throwing astd::string("bar"),std::runtime_exception("bar"), etc.).