Add anyhow to your Cargo.toml dependencies and use Result<T> as the return type for functions that can fail.
[dependencies]
anyhow = "1"
use anyhow::{anyhow, Result};
fn process_data() -> Result<String> {
if false {
return Err(anyhow!("Something went wrong"));
}
Ok("Success".to_string())
}
In your main function, use the ? operator to propagate errors automatically, letting anyhow handle the final panic if needed.