我正在阅读此页 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/