parts/4.7.ErrorHandling.md

4.7 Error Handling

The C++ language and standard library provides several mechanisms for error handling:

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

An 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.

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.