Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Panic Handling

Overview

Rust’s panic mechanism handles unrecoverable errors. catch_unwind allows controlled panic recovery.

Code Example

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

let result = panic::catch_unwind(|| {
    println!("About to panic...");
    panic!("This is a controlled panic!");
});

match result {
    Ok(_) => println!("No panic occurred"),
    Err(e) => {
        if let Some(s) = e.downcast_ref::<&str>() {
            println!("Caught panic: {}", s);
        } else {
            println!("Caught unknown panic");
        }
    }
}
}

Key Concepts

  • panic!: Unrecoverable error, unwinds stack
  • catch_unwind: Recovers from panics
  • Use Result for recoverable errors, panic for bugs

Learn More