☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

不应在 catch 子句外使用空 throw 表达式(throw;)

7.23 ID_rethrowOutOfCatch
目录 › next › previous

空 throw 表达式用于重新抛出当前捕获的异常,用在 catch 子句外是危险的,增大了流程控制的复杂性。

如果当前没有异常被捕获,空 throw 表达式会引发 std::terminate 函数的执行,导致程序异常终止。

示例:

void foo() {
    throw;     // Non-compliant
}

void bar() {
    try {
        throw;   // Non-compliant
    }
    catch (...) {   // Cannot catch ‘throw;’
        ....
    }
}

依据

ISO/IEC 14882:2003 15.1(6 8) ISO/IEC 14882:2003 15.3(9)-implementation ISO/IEC 14882:2011 15.1(8 9) ISO/IEC 14882:2011 15.3(9)-implementation

参考

MISRA C++ 2008 15-1-3
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.