Ch 9 — Error Handling
📖 https://doc.rust-lang.org/book/ch09-00-error-handling.html
Rust splits errors into unrecoverable (panic!) and recoverable (Result<T, E>). No exceptions.
panic! — unrecoverable
panic!("crash and burn");
let v = vec![1, 2, 3];
v[99]; // also panics (bounds check)
- Panics unwind the stack (running destructors) by default;
panic = "abort"in Cargo.toml for smaller binaries. RUST_BACKTRACE=1 cargo runfor a backtrace.- Panic when a bug/invariant violation makes continuing meaningless; use
Resultfor expected failures.
Result<T, E> — recoverable
enum Result<T, E> { Ok(T), Err(E) }
let file = match File::open("hello.txt") {
Ok(f) => f,
Err(e) => match e.kind() {
ErrorKind::NotFound => File::create("hello.txt").expect("create failed"),
other => panic!("open failed: {other:?}"),
},
};
Shortcuts
let f = File::open("hello.txt").unwrap(); // panic on Err
let f = File::open("hello.txt").expect("hello.txt should exist"); // panic w/ message (preferred)
let f = File::open("x").unwrap_or_else(|e| ...); // closure fallback (Ch 13)
? — the error-propagation operator
fn read_username() -> Result<String, io::Error> {
let mut s = String::new();
File::open("hello.txt")?.read_to_string(&mut s)?; // Err → early-return it
Ok(s)
}
// even shorter: fs::read_to_string("hello.txt")
?onErr(e)returnsErr(From::from(e))— auto-converts error types via theFromtrait. This is howBox<dyn Error>and custom error enums absorb everything.?also works onOption(propagatesNone), but you can’t mixResultandOptionin one function without converting (ok_or,.ok()).maincan return aResult:
fn main() -> Result<(), Box<dyn std::error::Error>> {
let f = File::open("hello.txt")?;
Ok(())
}
To panic or not
- Panic / expect: examples, prototypes, tests, and cases where you have more information than the compiler (
"127.0.0.1".parse().expect("hardcoded IP is valid")). - Result: anything a caller might reasonably want to handle — I/O, parsing, network.
- Encode invariants in types so bad states can’t be constructed (e.g., a
Guessstruct whosenew(value)validates 1..=100 once).
Gotchas
?only works in functions whose return type supports it (Result,Option, or otherFromResidualtypes) — using it in a()main is a compile error.unwrap()in library code is a smell; preferexpect("why this can't fail")so panics are self-documenting.- Ecosystem (beyond the book):
thiserrorfor library error enums,anyhowfor application code.