Panic Basics
In this page:
Triggering a Panic Explicitly
The panic! macro immediately stops normal program execution with the given message, used for situations that should truly never happen.
Example: Triggering a Panic Explicitly
fn main() {
let should_continue = true;
if !should_continue {
panic!("This should never happen");
}
println!("Program continued normally");
}
Login to try C/C++/Java/PHP code in the editor
Panics from Built-in Operations
Several standard operations panic automatically on invalid input, such as indexing an array out of bounds. This example shows a safe check before it would happen.
Example: Panics from Built-in Operations
fn main() {
let numbers = [1, 2, 3];
let index = 2;
if index < numbers.len() {
println!("Safe access: {}", numbers[index]);
} else {
println!("Index out of bounds, avoided panic");
}
}
Login to try C/C++/Java/PHP code in the editor
Panic vs Result
panic! is appropriate for programming bugs and truly unrecoverable states, while Result is appropriate for expected, recoverable failure conditions that callers should be able to handle.
Example: Panic vs Result
fn safe_divide(a: i32, b: i32) -> Result<i32, &'static str> {
if b == 0 {
Err("division by zero is an expected, recoverable error")
} else {
Ok(a / b)
}
}
fn main() {
match safe_divide(10, 0) {
Ok(v) => println!("{}", v),
Err(e) => println!("Handled gracefully: {}", e),
}
}
Login to try C/C++/Java/PHP code in the editor
Catching Panics with catch_unwind
In rare cases, std::panic::catch_unwind can catch a panic and prevent it from crashing the whole program, though this is uncommon and not a substitute for proper Result-based error handling.
Example: Catching Panics with catch_unwind
use std::panic;
fn main() {
let result = panic::catch_unwind(|| {
println!("About to panic inside catch_unwind");
panic!("controlled panic");
});
match result {
Ok(_) => println!("No panic occurred"),
Err(_) => println!("Panic was caught and handled"),
}
}
Login to try C/C++/Java/PHP code in the editor
- Using
panic!for ordinary, expected error conditions instead of returning aResultthat callers can handle gracefully. - Assuming a panic can always be caught and recovered from like an exception -- by default it unwinds and terminates the current thread's work.
- Not realizing indexing out of bounds, integer overflow (in debug builds), and
.unwrap()onNone/Errall trigger a panic automatically.
panic!("message")immediately stops normal execution and begins unwinding (or aborting) the program.- Common built-in operations like out-of-bounds indexing or
.unwrap()onNonetrigger a panic automatically. - Panics are meant for unrecoverable bugs, not for expected, everyday error conditions -- use
Resultfor those. RUST_BACKTRACE=1can be set when running a program to print a full backtrace when a panic occurs.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: