use std::panic;
fn main() {
/* The std::panic module’s catch_unwind function is crucial for error
handling in Rust:panic::catch_unwind(|| {...}): This function
attempts to execute the code within the closure.
*/
let result = panic::catch_unwind(|| {
println!("About to panic!");
panic!("Oops! A panic occurred.");
/* Normally, a panic would unwind the stack and potentially cause the program to
terminate, but here, it's caught by catch_unwind.
*/
});
match result {
Ok(_) => println!("No panic occurred."),
Err(_) => println!("A panic was caught."),
}
/* This mechanism is essential for building robust applications
that need to handle panics gracefully, especially in multithreaded
or networked environments where a panic in one part should not
affect the entire system.
*/
let result = panic::catch_unwind(|| {
println!("No need to panic!");
});
match result {
Ok(_) => println!("No panic occurred."),
Err(_) => println!("A panic was caught."),
}
}