Skip to content

Instantly share code, notes, and snippets.

@vmx
Last active May 12, 2021 03:25
Show Gist options
  • Save vmx/5d952e751a7e20b62cd45385bd9fea62 to your computer and use it in GitHub Desktop.
Save vmx/5d952e751a7e20b62cd45385bd9fea62 to your computer and use it in GitHub Desktop.
Minimal example for "Printing panics in Rust" blog post
use std::panic;
fn main() {
let result = panic::catch_unwind(|| {
panic!("Some nice cause for this panic".to_string());
});
if let Err(panic) = result {
match panic.downcast::<String>() {
Ok(panic_msg) => {
println!("panic happened: {}", panic_msg);
}
Err(_) => {
println!("panic happend: unknown type.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment