Why doesn’t the following doesn’t handle the exception that was rethrown? I tried all the combinations but none of them would show the output in last catch so I’m confused!
Derived D;
try {
throw D;
} catch ( const Derived &d) {
throw;
} catch (const Base &b) {
cout << "caught!" << endl;
}
Derived D;
try {
throw D;
} catch ( const Derived d) {
throw;
} catch (const Base b) {
cout << "caught!" << endl;
}
Derived D;
try {
throw D;
} catch ( const Derived d) {
throw;
} catch (const Base &b) {
cout << "caught!" << endl;
}
Derived D;
try {
throw D;
} catch ( const Derived &d) {
throw;
} catch (const Base b) {
cout << "caught!" << endl;
}
The re-throw is not handled by the same
try-catchblock. It’s thrown up to the calling scope.In
[except.throw](2003 wording):and:
Your
tryblock has exited, so its handlers are not candidates. Thus, none of thecatchblocks in your code may handle the re-throw.Admittedly this is rather confusing wording.