Skip to content

Page 100: Explanation of exception inheritance hierarchy handling in catch blocks is wrong. #75

@WraithGlade

Description

@WraithGlade

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());
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions