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;
  4. let result = panic::catch_unwind(|| {
  5. println!("hello!");
  6. });
  7. assert!(result.is_ok());
  8. let result = panic::catch_unwind(|| {
  9. panic!("oh no!");
  10. });
  11. assert!(result.is_err());
  12. }
  • 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.