”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 现代C++中异常说明符是否仍有用?

现代C++中异常说明符是否仍有用?

发布于2025-04-16
浏览:254

Are Exception Specifiers Still Relevant in Modern C  ? 
我应该在C?

While these specifiers can convey intent, their practical use is questionable due to several factors:

Limitations in Enforcement

void foo() throw(); // guaranteed not to throw an exception
void bar() throw(int); // may throw an exception of type int
void baz() throw(...); // may throw an exception of some unspecified type
对违法行为的严重惩罚

如果函数违反异常指定器,则标准行为是终止程序的标准行为。在所有情况下,这种严酷的反应可能并不是可取的。

在开发环境中不一致的处理

一些开发环境,例如vs.net,将(x)视为投掷(x),将(...)当作(...),强调了(...),对标准抗拒。 specifiers 除了上述问题之外,几个参数提倡使用异常指定器:

与模板的兼容:

异常规格可能会阻碍模板代码的开发,使模板的开发可能会阻碍模板的异常行为行为行为pravivion praviviors &&&固。可扩展性:[

指定预期异常可能会随着时间的推移而限制代码的演变。

与遗留代码的兼容性:处理可能的例外情况很难预测可能的异常,可能会导致程序终止。 reserving exceptions for exceptional scenarios.

When Exception Specifiers May Be Useful

Despite the general recommendation against using exception specifiers, they can still be beneficial when:

A library throws only its own exceptions, allowing for clear intent specification.但是,这种用例在实践中很少见。

最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3