Pybites Logo Rust Platform

Custom Error Types

Medium +3 pts

🎯 In Python, you define custom exceptions by subclassing:

class CalcError(Exception):
    pass

class DivisionByZero(CalcError):
    pass

class ParseError(CalcError):
    def __init__(self, token):
        self.token = token

Different error types, but they're all classes in an inheritance hierarchy. You catch the base class or specific ones.

Rust uses enums for error types instead of class hierarchies:

#[derive(Debug, PartialEq, Eq)]
enum CalcError {
    DivisionByZero,
    ParseInt(String),
    BadFormat,
}

Each variant is a distinct error case. The enum carries data when needed (ParseInt(String) stores the bad token). No inheritance, no base class — the match expression handles each case exhaustively.

The ? operator

In exercise 09, you returned Result manually. Rust's ? operator chains operations that might fail:

fn process(input: &str) -> Result<Output, AppError> {
    let parsed = parse(input)?;   // if Err, return it immediately
    let checked = validate(parsed)?;   // if Err, return it immediately
    transform(checked)
}

? is like Python's implicit exception propagation, but explicit — you see exactly where errors can occur and how they flow.

map_err for error conversion

When the error types don't match directly, map_err converts:

user_input
    .parse::<f64>()
    .map_err(|_| AppError::InvalidInput(user_input.to_string()))

This turns a std::num::ParseFloatError into your AppError::InvalidInput. Python's equivalent would be catching one exception and raising another.

Login to see the full task and start coding.

This is a premium exercise

Log in to unlock the full exercise and start coding.

Login to access this exercise