c++ - 关于 C++ 异常。函数()抛出()

标签 c++ exception throw

我正在阅读此页 http://www.cplusplus.com/doc/tutorial/exceptions.html 它说如果我写 function() throw();该函数不能抛出异常。我尝试在 msvc 2005 中编写 throw()、throw(int)、throw(),但什么也没有。每个都有完全相同的结果。没有什么。我扔了 int、char* 和另一种类型,它们都以同样的方式被捕获。看起来 throw 根本不影响它。 function() throw() 实际上做了什么?

最佳答案

参见 this article有关 C++ 异常规范和 Microsoft 实现的详细信息:

Microsoft Visual C++ 7.1 ignores exception specifications unless they are empty. Empty exception specifications are equivalent to __declspec(nothrow), and they can help the compiler to reduce code size.

[...] If it sees an empty exception specification, it will assume you know what you are doing and optimize away the mechanics for dealing with exceptions. If your function throws anyway - well, shame on you. Use this feature only if you are 100% positive your function does not throw and never will.

关于c++ - 关于 C++ 异常。函数()抛出(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/604372/

相关文章:

java - 捕获 IllegalArgumentException?

c++ - 抛出一个右值

javascript - 如何在 Javascript 循环中触发的回调函数中抛出一次错误?

c++ - StretchBlt 收缩损坏

c++ - 编译器如何使用编译器生成的临时文件确定函数所需的堆栈大小?

c++ - 如何在 CMake Superbuild 中查找 dll/so 文件

java - 第二次创建位图时内存不足异常

c++ - 为什么 std::setprecision 显示不存在的精度位以及如何查看真正的 float

Java swing 异常处理程序

java - 如何使用异常?