In the following text:
Exceptions use these inheritance relationships to determine whether a handler catches an exception. Handlers will catch a given type and any of its parents' types.
The bold sentence above says the opposite of the truth. In reality, handlers catch a given type and any of its children's types. This is a very important difference. Newbies will be confused.
The following function can be called to prove that it is the children's types that are caught:
#include <stdexcept>
void test_exception_hierarchy_catch() {
try {
throw std::exception("This exception is an instance of std::exception.\n");
}
catch (const std::runtime_error& ex) {
printf("Caught an std::runtime_error.\nThe message is: %s\n", ex.what());
}
catch (const std::exception& ex) {
printf("Caught an std::exception.\nThe message is: %s\n", ex.what());
}
}
In the following text:
The bold sentence above says the opposite of the truth. In reality, handlers catch a given type and any of its children's types. This is a very important difference. Newbies will be confused.
The following function can be called to prove that it is the children's types that are caught: