【发布时间】:2012-10-28 17:30:38
【问题描述】:
我有一些看起来像这样的 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;
}
}
如果构造函数发现包含课程成绩的gr 无效,则GradedCourse 构造函数可能会抛出异常。如果发生此类异常,是否会执行 try 块内的任何进一步语句?我可以确定这样的异常不会导致尝试将GradedCourse 插入coursesTaken(这是一个 STL 集)吗?我搜索了 Stack Overflow 和 Google,但都没有成功。
【问题讨论】:
-
是的,如果第一条语句抛出异常,您的第二条语句将永远不会被执行。
-
@HunterMcMillen 如果您想回答问题,为什么不发布答案而不是发表评论?
-
注意(因为我看到太多了):try/catch/rethrow 是不必要的,
throw(...)规范也是如此...