I need to catch a panic so that it doesn't exit the the program. For example, how to catch a panic here and print "Hello, World"?:
fn main() {
let v = vec![1, 2, 3];
v[99];
println!("Hello, World");
}
You can use std::panic::catch_unwind
to, well, catch unwinding panics, but do make sure to read the documentation first:
fn main() {
let v = vec![1, 2, 3];
let panics = std::panic::catch_unwind(|| v[99]).is_err();
assert!(panics);
println!("Hello, World");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With