关于 c++ 例外情况。 函数() 抛出()
我正在阅读此页面http://www.cplusplus.com/doc/tutorial/exceptions。 html 它说如果我写 function() throw(); 该函数中不能抛出任何异常。 我尝试在 msvc 2005 中编写 throw()、 throw(int)、 throw() ,但什么也没写。 每个都有完全相同的结果。 没有什么。 我扔了 int、char*、另一种类型,它们都以同样的方式被捕获。 看来投掷根本不影响它。 function() throw() 实际上做了什么?
i am reading this page http://www.cplusplus.com/doc/tutorial/exceptions.html
it says if i write function() throw(); no exceptions can be thrown in that function. I tried in msvc 2005 writing throw(), throw(int), throw() and nothing at all. each had the exact same results. Nothing. I threw int, char*, another type and it was all caught the same way. It looks like throw doesnt affect it at all. What does function() throw() actually do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有关 C++ 异常规范和 Microsoft 实现的详细信息,请参阅本文:
See this article for details on C++ exception specifications and Microsoft's implementation:
您会发现该版本的 VC++ 没有强制执行规范异常。 我相信这被记录为与标准的差异。
然而,异常规范通常不是一个好主意。 如果程序在符合标准的实现中违反了这些规定(VS 2005 中的 VC++ 不属于这种情况),系统应该捕获它。 这意味着该规范不是编译器优化提示,而是强制编译器使用额外的长度,有时会生成次优代码。
请参阅Boost 基本原理,了解备受推崇的 Boost 项目为何不这样做的原因使用异常规范。 这就是 Boost,它是用语言的高级部分做奇怪而有用的事情的典型代表。
What you're finding is that that version of VC++ didn't enforce specification exceptions. I believe that was documented as a variance from the standard.
However, exception specifications are usually not a good idea. If a program violates them in a standards-conforming implementation (which the VC++ from VS 2005 was not in this case), the system is supposed to catch it. This means that the specification is not a compiler optimization hint, but rather forces the compiler to go to extra lengths, and sometimes produce suboptimal code.
See the Boost rationale for reasons why the highly regarded Boost project does not use exception specifications. This is Boost, which is something of the poster child for doing weird and useful things with advanced parts of the language.
引用自对异常规范的实用看法:
请参阅链接了解完整详细信息。
Quoting from A Pragmatic Look at Exception Specifications:
See the link for the full details.
抛出异常是不够的,您需要一个
try {} catch()
块来捕获异常。 如果您没有捕获异常,则会调用std::terminate()
并且您的程序会突然退出。 花点时间看看这个。Throwing an exception is not enough, you need a
try {} catch()
block to catch exceptions. If you don't catch exceptions,std::terminate()
is called and your program exits abruptly. Take some time out and have go at this.抛出规范的设计有两个目的:
作为接口实现和接口用户之间的契约 - 您声明可以从您的方法抛出哪些异常,有些人认为它是接口的一部分。 (契约)Ala 在 Java 中检查异常。
作为一种向编译器发出信号的方式,表明它可以应用某些优化,以防方法/过程不会引发异常(设置异常处理会花费一些费用)
抛出 throw() 子句中未指定的异常是一个错误,但是实现在任何时候都不需要验证为您服务。 事实上,它甚至无法验证,因为它包括子例程调用的子例程中所有可能的异常。 (可能来自其他模块)这甚至在单个模块内都是不可能的,因为很容易减少到停止问题:)
throw specifications are designed for two purposes:
To serve as a contract between interface implemented and interface user - you state which exceptions can be throwned from your method, some people consider it part of an interface. (contract) Ala checked exceptions in Java.
As a way to signal the compiler that it can apply certain optimizations in case no exceptions can be thrown from a method/procedure (setting up exception handling costs something)
Throwing an exception not specified in throw() clause is a mistake, however at no point is the implementation required to verify it for you. In fact it's not even possible to verify, as it includes all the possible exceptions from subroutines your subroutine calls. (possibly from other modules) It is not even possible within a single module, as is easily reduced to a halting problem :)