In C++, is it valid for the constructor of an object that is going to be thrown itself throw an exception? In other words, are we in the throw yet while we are still constructing the object to throw?
struct Error {
Error() {
if (someCondition()) {
throw anotherObject();
}
}
};
void test() {
throw Error();
}
The throw expression would need to be
throw Error();, but yes, this is valid.Before the
Errorobject can be thrown, it must be constructed. That is, the subexpressionError()must be evaluated before thethrowoperator can be evaluated in the full expression. If evaluation of the subexpressionError()itself throws an exception, the rest of the full expression (i.e., thethrow) would not be evaluated.