an unnamed function object. Exception handling Exception handling is used to communicate the existence of a runtime problem or error from where it was detected to where the issue can be handled. It permits this to be done in a uniform manner and separately from the main code, while detecting all errors. Should an error occur, an exception is thrown (raised), which is then caught by the nearest suitable exception handler. The exception causes the current scope to be exited, and also each outer scope (propagation) until a suitable handler is found, calling in turn the destructors of any objects in these exited scopes. At the same time, an exception is presented as an object carrying the data about the detected problem. Some C++ style guides, such as Google's, LLVM's, and Qt's forbid the usage of exceptions. The exception-causing code is placed inside a try block. The exceptions are handled in separate catch blocks (the handlers); each try block can have multiple exception handlers, as it is visible in the example below. #include <iostream> #include <vector> #include <stdexcept> int main() { try { std::vector<int> vec{3, 4, 3, 1}; int i{vec.at(4)}; // Throws an exception, std::out_of_range (indexing for vec is from 0-3 not 1-4) } // An exception handler, catches std::out_of_range, which is thrown by vec.at(4) catch (const std::out_of_range &e) { std::cerr << "Accessing a non-existent element: " << e.what() << '\n'; } // To catch any other standard library exceptions (they derive from std::exception) catch (const std::exception &e) { std::cerr << "Exception thrown: " << e.what() << '\n'; } // Catch any unrecognised exceptions (i.e. those which don't derive from std::exception) catch (...) { std::cerr << "Some fatal error\n"; } } It is also possible to raise exceptions purposefully, using the throw keyword; these exceptions are handled in the usual way. In some cases, exceptions cannot be used due to technical reasons. One such example is a critical component of an embedded system, where every operation must be guaranteed to complete within a specified amount of time. This cannot be determined with exceptions as no tools exist to determine the maximum time required for an exception to be handled. Unlike signal handling, in which the handling function is called from the point of failure, exception handling exits the current scope before the catch block is entered, which may be located in the current function or any of the previous function