Diagnostics library
A guide to Clause 19: what problems the <stdexcept> exception classes, assert, errno, <system_error>, <stacktrace> and <debugging> each solve, how they divide the work, and how they relate to exceptions and contracts in the language.
Key points
- Clause 19 contains six groups of tools: predefined exception classes,
assert,errno,<system_error>,<stacktrace>(C++23) and<debugging>(C++26). - Bugs in the program itself (precondition violations) are reported with assertions or contracts; failures caused by the environment, such as a missing file or a timeout, are reported with exceptions or error codes.
- Interfaces such as
<filesystem>usually provide both a version that throwssystem_errorand a version that reports through anerror_code&out-parameter, both sharing the same set of error codes. throw,try,std::exceptionand contracts aren't in this clause; see [except], [support.exception] and [basic.contract] respectively.
[diagnostics] ¶1 says this clause describes components that C++ programs may use to detect and report error conditions. Table 49 in ¶2 lists six groups:
| Subclause | Header | Problem solved |
|---|---|---|
| [std.exceptions] | <stdexcept> |
A set of predefined exception types, in two branches: logic errors and runtime errors |
| [assertions] | <cassert> |
The assert macro, which can be turned off with NDEBUG |
| [errno] | <cerrno> |
C- and POSIX-style thread-local error numbers |
| [syserr] | <system_error> |
Wrapping integer error codes from the operating system or low-level APIs in portable, comparable, extensible types |
| [stacktrace] C++23 | <stacktrace> |
Getting the current call stack at run time |
| [debugging] C++26 | <debugging> |
Interacting with a debugger: setting breakpoints, detecting whether a debugger is present |
The other parts of error handling are elsewhere. The language mechanism of exceptions (throw, try, stack unwinding) is in [except]; the std::exception base class, bad_alloc, exception_ptr and terminate are in the language support library, [support.exception]; std::expected is in the general utilities library; and the contract assertions pre, post and contract_assert are in the core language, [basic.contract].
How the work is divided#
| Nature of the error | How it is reported |
|---|---|
| A bug in the program itself, such as a precondition violation | assert, contract assertions; when the library chooses to check, the logic_error branch is thrown |
| A failure caused by the environment, such as a missing file or a timeout | Throw from the runtime_error branch or system_error; or an error_code out-parameter or return value; C and POSIX interfaces use errno |
| Locating problems after the fact | stacktrace records the call path, and <debugging> hands over to the debugger |
The standard doesn't mandate this division, but the library's design reflects it:
- Violating the Preconditions of a standard library function is undefined behavior ([structure.specifications]) and doesn't throw
logic_error. C++26 hardened implementations turn violations of some hardened preconditions into contract violations. Thelogic_errorbranch is used where the library actively checks and reports, for example theout_of_rangethrown byvector::at. - For expected environmental failures, the library often provides both ways. Most operations in
<filesystem>have two overloads: one throwsfilesystem_error(derived fromsystem_error), and one takes anerror_code&out-parameter and doesn't throw. errnois a legacy facility for compatibility with C. The components of [syserr] don't modifyerrno([syserr.general] ¶2); the two connect through theerrcenumeration andgeneric_category().
Freestanding implementations#
Freestanding implementations (implementations that may lack operating system support) must also provide the E* macros in <cerrno>, the errc enumeration in <system_error>, and all of <debugging>. errno itself, <stdexcept> (which depends on std::string), and <stacktrace> are provided only by hosted implementations.
Changes since C++23#
<stacktrace>was added in C++23 (P0881R7), with formatting support from P2693R1.assertbecame a variadic macro in C++26 (P2264R7), so commas in template arguments no longer need extra parentheses.<debugging>was added in C++26 (P2546R5), andis_debugger_presentwas later specified as a replaceable function (P2810R4).- The constructors of all exception classes in
<stdexcept>areconstexprin the current draft, to go with C++26 allowing exceptions to be thrown during constant evaluation (P3068R6). error_codegot astd::formatterspecialization ([syserr.fmt]), which is new wording after C++23.
At the time of writing, Clang 23 + libc++ doesn't yet provide <stacktrace> or <debugging>, and doesn't support throwing exceptions during constant evaluation.
Contents
- 19.1General[diagnostics.general]
- 19.2Exception classes[std.exceptions]done
- 19.2.1General[std.exceptions.general]
- 19.2.2Header <stdexcept> synopsis[stdexcept.syn]
- 19.2.3Class logic_error[logic.error]
- 19.2.4Class domain_error[domain.error]
- 19.2.5Class invalid_argument[invalid.argument]
- 19.2.6Class length_error[length.error]
- 19.2.7Class out_of_range[out.of.range]
- 19.2.8Class runtime_error[runtime.error]
- 19.2.9Class range_error[range.error]
- 19.2.10Class overflow_error[overflow.error]
- 19.2.11Class underflow_error[underflow.error]
- 19.3Assertions[assertions]done
- 19.4Error numbers[errno]done
- 19.5System error support[syserr]done
- 19.5.1General[syserr.general]
- 19.5.2Header <system_error> synopsis[system.error.syn]
- 19.5.3Class error_category[syserr.errcat]
- 19.5.4Class error_code[syserr.errcode]
- 19.5.5Class error_condition[syserr.errcondition]
- 19.5.6Comparison operator functions[syserr.compare]
- 19.5.7System error hash support[syserr.hash]
- 19.5.8Class system_error[syserr.syserr]
- 19.6Stacktrace[stacktrace]draft
- 19.7Debugging[debugging]done