The C++ language and standard library provides several mechanisms for error handling:
std::string::npos
std::expected<T,E>
or std::optional<T>
for a return type T
. The error type E
can use std::error_code
, for example.errno
, or as an objects status as with std::istream
’s flags. Note, that the standard library provides std::error_code
that can be used to map errno
values for further processing.Be aware, that many parts of the standard library specify preconditions and undefined behavior results if those aren’t met, for example, std::vector<int>::front()
requires a non-empty vector, when called.
Exceptions allow for errors to be propagated up the call chain. Even though the standard library provides a type hierarchy derived from std::exception
, any copyable type can be thrown. Throwing an exception due to a detected error situation allows the error to be handled at an appropriate level in a corresponding catch block. As the exception propagates to its handler, local objects are destroyed appropriately in reverse order of their construction; this mechanism is known as stack unwinding. A search for a matching handler stops at
main()
,noexcept
, andAn exception propagated from constructors of non-local variables and destructors of variables with static storage duration can never have a matching handler.
Failing to provide a matching handler on the call chain for an exception thrown causes a call to std::terminate()
and the program terminates.
When calling non-returning program termination functions like abort()
, std::terminate()
, or exit()
, the program terminates without stack unwinding.
#TODO: shorten again by moving to 6.36? talk about coroutines… see what Paul finds out
An exception propagating out of a coroutine causes the coroutine to end in an unresumable state and the exception is not further propagated.
Distinct mechanism for IEEE floating point exceptions
defer to C
Used by any library functions external to C++, and by some C++ libraries.
errno is a thread-local single value that, if non-zero, reports an error that has occurred and is returned by function as the return value.
Any external can be called using error_code which will wrap the errno.
error_code is an extension or errno but is not a global variable and can work with classes for better reporting.
In addition to that list, an operating system might use the “signal” mechanism to notify a running C++ program. A signal-handler can be defined to act asynchronously upon a sent signal that isn’t ignored.