Step 5 of 6
Passing errors up
Real programs are layers: main calls load_config, which calls parse_line, which calls parse_int. When the bottom layer fails, each layer above has two honest choices:
- Handle it (use a default, retry, skip the item), or
- Pass it up unchanged, usually with an early
return.
The one dishonest choice is swallowing it: returning a made-up value as if nothing went wrong. That's how bugs travel far from where they started.
auto n = parse_int(part);
if (!n) return std::nullopt; // pass it up
total += *n;
Your turn: parse_int is written for you. Write std::optional<int> sum_csv(std::string_view s) that sums comma-separated integers like "3,4,-2". If any part fails to parse, the whole sum fails.