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

@WraithGlade WraithGlade commented Dec 22, 2019

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 JLospinoso closed this in 2b41efa Jan 1, 2020
@JLospinoso

This comment has been minimized.

Copy link
Owner

@JLospinoso JLospinoso commented Jan 1, 2020

Thanks, @WraithGlade!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.