Rust Reference ยท Errors, Panics and Process
Panic, unwind and abort in Rust
What a Rust panic does: the message and exit code 101, stack unwinding and Drop, catch_unwind, panic=abort, panics in threads and double panics.
A panic is Rust's response to a bug it cannot recover from: an out-of-bounds index, an unwrap() on None, a failed assert!, an integer overflow in a debug build, or an explicit panic!. The default sequence is: print a message with the source location to standard error, then unwind the stack of the current thread, running Drop for every live value on the way out. If the panic reaches the top of the main thread, the process exits with status 101.
Unwinding is the default strategy. The alternative, abort, ends the process immediately without running destructors. You choose between them in Cargo.toml with panic = "abort" in a profile.
let v = vec![1, 2, 3]; let i = 7; println!("{}", v[i]); // panics: index out of bounds
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
How it works
| Stage | What happens |
|---|---|
| Raise | panic! (or a failed check) builds a payload (usually a &str or String) and calls the panic hook. |
| Hook | The default hook prints thread 'main' panicked at src/main.rs:L:C: plus the message. std::panic::set_hook replaces it; take_hook removes it. |
| Unwind | The stack is walked frame by frame; locals are dropped in reverse order of declaration. |
| End | The thread ends. For a spawned thread, join returns Err(payload). For the main thread the process exits with code 101. |
Abort mode: with panic = "abort" the hook still runs, then the process aborts (typically with SIGABRT). No Drop code runs, catch_unwind cannot catch anything, and the binary is a little smaller and faster.
Double panic: if a destructor panics while the thread is already unwinding, Rust aborts the process. Never panic inside Drop if it can be avoided.
Usage notes
catch_unwindis meant for boundaries, such as stopping a panic from crossing an FFI call or isolating a plugin. It is not an exception system. The closure must beUnwindSafe; wrap it inAssertUnwindSafewhen you are sure the state is consistent afterwards.- A mutex locked by a thread that panics becomes poisoned; later
lock()calls returnErrso you notice the data may be half-updated. panic::resume_unwind(payload)re-raises a caught panic without running the hook again.- Panics in a spawned thread do not stop other threads, but joining that thread returns the error. If nobody joins, the panic message on stderr is the only sign.
- Integer overflow panics only when overflow checks are enabled (debug builds by default). Release builds wrap silently unless you enable
overflow-checks. - Set
RUST_BACKTRACE=1to print a backtrace with the panic message. - Since Rust 1.81 a panic that unwinds out of a function marked
extern "C"aborts the process instead of causing undefined behavior; useextern "C-unwind"when unwinding across the boundary is intended. - Code that must never panic (a destructor, a signal handler) should use fallible methods such as
get,checked_addandtry_lock.
Related
panic!,std::panic,Drop, Error handling overview, Exit codes- Lessons: Panic, Drop and RAII