m06-error-handling

Standardize C++ error handling with exceptions, error codes, optional, expected, and assertions.

14|3|Updated Jan 25, 2026
One-click install
npx skills add https://github.com/13eholder/Modern-Cpp-Skills --skill m06-error-handling-13eholder
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m06-error-handling
Source: https://github.com/13eholder/Modern-Cpp-Skills/tree/main/m06-error-handling
Command: npx skills add https://github.com/13eholder/Modern-Cpp-Skills --skill m06-error-handling-13eholder

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

C++ error handling is often complex, fragmented, and easy to mishandle, leading to brittle code and subtle bugs. This Skill provides a cohesive set of patterns and decisions to make error handling explicit, safe, and maintainable across codebases.

Core Features & Use Cases

  • Central guidance on when to use exceptions, error codes, std::optional, and std::expected.
  • Clear strategies for noexcept and RAII to ensure resource safety during errors.
  • Real-world scenarios across library boundaries and application code to reduce silent failures and improve debuggability.

Quick Start

Write a small function that returns std::optional<T> on failure and refactor it to use std::expected<T, E> with proper noexcept where appropriate.

Frequently Asked Questions about m06-error-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
When should I use std::optional vs std::expected for C++ error handling?

Use std::optional for C++ error handling when a function might return no value without a specific failure cause, and use std::expected when you need to return a value or propagate a detailed error code. This distinction makes failures explicit and debuggable.

How do I use noexcept and RAII to ensure resource safety during exceptions in C++?

Use noexcept and RAII to ensure resource safety during C++ exceptions by marking non-throwing functions noexcept and letting object destructors automatically release resources. This prevents resource leaks and silent failures when unwinding the call stack.

What is the best way to handle errors across C++ library boundaries?

The best way to handle errors across C++ library boundaries is to standardize error handling patterns explicitly, choosing between exceptions, error codes, or std::expected based on whether the error is recoverable or fatal, ensuring safe integration and improved debuggability.

How do I distinguish between recoverable errors and fatal failures in C++?

Distinguish recoverable errors from fatal C++ failures by using try/catch, std::optional, or std::expected for recoverable conditions, and applying assert or noexcept for fatal program states that should immediately halt execution instead of propagating silently.

How do I refactor C++ code returning std::optional to use std::expected?

Refactor C++ code returning std::optional to std::expected by changing the return type to std::expected<T, E> and replacing empty optional returns with explicit error types. This provides clear failure context and applies noexcept where appropriate to guarantee non-throwing behavior.