Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
WraithGlade opened this issue Dec 22, 2019 · 1 comment

Comments

@WraithGlade
Copy link

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());
	}
}
@JLospinoso
Copy link
Owner

Thanks, @WraithGlade!

kruschk pushed a commit to kruschk/ccc that referenced this issue Apr 26, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants