我有一些如下所示的 C++ 代码:
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;
}
}
如果构造函数发现包含类(class)成绩的 gr
无效,则 GradedCourse
构造函数可能会引发异常。如果发生此类异常,try
block 内的任何其他语句是否会被执行?我能否确定此类异常不会导致尝试将 GradedCourse
插入 coursesTaken
(这是一个 STL 集)?我在 Stack Overflow 和 Google 上都进行了搜索,但没有取得太大成功。
最佳答案
没有。
如果 GradedCourse c(cVal, gr);
抛出异常,则 try
block 内的任何其他内容都不会被执行。
关于c++ - try block 中一条语句中的异常是否会导致控制流绕过 try block 中的任何剩余语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13111238/