I have some C++ code that looks like this:
void Student::addCourse(Course cVal, string gr) throw(...) {
try {
GradedCourse c(cVal, gr); // If an exception is thrown here...
coursesTaken.insert(c); // will this statement be executed?
} catch(...) {
throw;
}
}
The GradedCourse constructor may throw an exception if gr, which contains a grade for a course, is found to be invalid by the constructor. If such an exception occurs, will any further statements inside the try block be executed? Can I be sure that such an exception will result in no attempt to insert the GradedCourse into coursesTaken (which is an STL set)? I’ve searched both Stack Overflow and Google without much success.
No.
If
GradedCourse c(cVal, gr);throws an exception, nothing else inside thetryblock will be executed.