First, let's briefly review how C++ exceptions are thrown and caught. In the following example, we will throw an exception from a function and then catch the exception in our main() function:
#include <iostream>#include <stdexcept>void foo(){ throw std::runtime_error("The answer is: 42");}int main(void){ try { foo(); } catch(const std::exception &e) { std::cout << e.what() << '\n'; } return 0;}
As shown in the preceding example, we created a function called foo() that throws an exception. This function is called in our main() function inside a try/catch block, which is used to catch any exceptions that might be thrown by the code executed inside the try block, which in this case is the foo() function. When the exception is ...