意外重新抛出¶
ID: cpp/rethrow-no-exception
Kind: problem
Security severity:
Severity: warning
Precision: high
Tags:
- reliability
- correctness
- exceptions
Query suites:
- cpp-security-and-quality.qls
C++ throw
表达式可以采用多种形式。一种形式抛出一个新的异常,而另一种形式则重新抛出当前异常。在后一种情况下,如果当前没有异常,则程序将终止。在异常处理上下文之外存在重新抛出操作通常是由于程序员不知道要抛出哪种异常。
建议¶
将 throw
表达式更改为抛出特定类型的异常。
示例¶
void bad() {
/* ... */
if(error_condition)
throw;
}
void good() {
/* ... */
if(error_condition)
throw std::exception("Something went wrong.");
}
参考¶
开放标准:C++ 编程语言标准草案 n3337 [except.throw],第 9 章,第 380 页。