Catching the Stack Unwinding

By default, a panic will cause the stack to unwind. The unwinding can be caught:

1. #![allow(unused)]
2. fn main() {
3. use std::panic;

5. let result = panic::catch_unwind(|| {
6. println!("hello!");
7. });
8. assert!(result.is_ok());

10. let result = panic::catch_unwind(|| {
11. panic!("oh no!");
12. });
13. assert!(result.is_err());
14. }
  • This can be useful in servers which should keep running even if a single request crashes.
  • This does not work if panic = 'abort' is set in your Cargo.toml.