Custom Error Types बनाना
In this page:
#[derive(Debug)]
enum NameError {
Variant(Type),
}
impl std::fmt::Display for NameError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "message")
}
}
impl std::error::Error for NameError {}
Defining a Custom Error Enum
एक custom error type आमतौर पर एक enum होता है जो हर distinct तरीके को list करता है जिससे एक operation fail हो सकता है, callers को सिर्फ एक generic string के बजाय structured information देते हुए।
उदाहरण: Defining a Custom Error Enum
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define an enum `MathError` listing its possible variants
enum MathError {
DivideByZero,
NegativeSquareRoot,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `err`, set to `MathError::DivideByZero`
let err = MathError::DivideByZero;
// Print "{:?}" to the console, substituting in `err` (with a trailing newline)
println!("{:?}", err);
}
Login to try C/C++/Java/PHP code in the editor
Returning a Custom Error from a Function
Functions custom error type उपयोग करके Result<T, MyError> return कर सकते हैं, compiler को यह enforce करने देते हुए कि हर failure case enum में represented है।
उदाहरण: Returning a Custom Error from a Function
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define an enum `MathError` listing its possible variants
enum MathError {
DivideByZero,
}
// Define the function `divide` taking `a`, `b` and returning a `Result<f64, MathError>`
fn divide(a: f64, b: f64) -> Result<f64, MathError> {
// Check whether `b == 0.0`
if b == 0.0 {
Err(MathError::DivideByZero)
// Otherwise, run this branch
} else {
Ok(a / b)
}
}
// Entry point: execution of the program starts here
fn main() {
// Print "{:?}" to the console, substituting in `divide(10.0, 0.0)` (with a trailing newline)
println!("{:?}", divide(10.0, 0.0));
}
Login to try C/C++/Java/PHP code in the editor
Implementing Display for Readable Messages
किसी custom error type के लिए std::fmt::Display implement करना इसे {} से print करते समय सिर्फ raw Debug output के बजाय एक साफ़, human-readable message देता है।
उदाहरण: Implementing Display for Readable Messages
// Bring `std::fmt` into scope
use std::fmt;
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define an enum `MathError` listing its possible variants
enum MathError {
DivideByZero,
}
// Implement methods on `fmt`
impl fmt::Display for MathError {
// Define the function `fmt` taking `&self`, `f` and returning a `fmt::Result`
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Match on `self` and branch by pattern
match self {
MathError::DivideByZero => write!(f, "cannot divide by zero"),
}
}
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `err`, set to `MathError::DivideByZero`
let err = MathError::DivideByZero;
// Print "{}" to the console, substituting in `err` (with a trailing newline)
println!("{}", err);
}
Login to try C/C++/Java/PHP code in the editor
Matching on a Custom Error
चूंकि एक custom error एक enum है, callers हर specific failure kind पर अलग response देने के लिए इस पर match कर सकते हैं, सिर्फ एक single generic error message रखने के बजाय।
उदाहरण: Matching on a Custom Error
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define an enum `MathError` listing its possible variants
enum MathError {
DivideByZero,
NegativeSquareRoot,
}
// Define the function `check` taking `n` and returning a `Result<f64, MathError>`
fn check(n: f64) -> Result<f64, MathError> {
// Check whether `n < 0.0`
if n < 0.0 {
Err(MathError::NegativeSquareRoot)
// Otherwise, run this branch
} else {
Ok(n.sqrt())
}
}
// Entry point: execution of the program starts here
fn main() {
// Match on `check(-4.0)` and branch by pattern
match check(-4.0) {
Ok(v) => println!("Root: {}", v),
Err(MathError::NegativeSquareRoot) => println!("Cannot take root of negative number"),
Err(MathError::DivideByZero) => println!("Divide by zero"),
}
}
Login to try C/C++/Java/PHP code in the editor
- एक proper enum के बजाय हर error के लिए
Stringउपयोग करना, callers की specific failure kinds पर match करने की क्षमता खोते हुए। std::fmt::Display(और अक्सरstd::error::Error) implement करना भूल जाना ताकि custom error type nicely print हो और दूसरे error-handling code के साथ interoperate करे।- एक custom error enum define करना लेकिन कभी
Debugderive या implement न करना, जो ज़्यादातर error-handling code expect करता है।
- एक custom error type आमतौर पर हर distinct तरह की failure के लिए एक variant वाला enum है।
std::fmt::Displayimplement करना error type को एक readable, user-facing message देता है।std::error::Errorimplement करना आपके custom error को wider Rust error-handling ecosystem के साथ interoperate करने देता है।- Custom error types callers को सिर्फ एक generic message string के बजाय specific failure kinds पर
matchकरने देते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: