关于 c++ 例外情况。 函数() 抛出()

发布于 2024-07-14 05:56:40 字数 353 浏览 14 评论 0原文

我正在阅读此页面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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你与昨日 2024-07-21 05:56:40

有关 C++ 异常规范和 Microsoft 实现的详细信息,请参阅本文

Microsoft Visual C++ 7.1 会忽略异常规范,除非它们为空。 空异常规范相当于__declspec(nothrot),它们可以帮助编译器减少代码大小。

[...] 如果它看到一个空的异常规范,它会假设您知道自己在做什么,并优化处理异常的机制。 如果你的函数无论如何都会抛出 - 好吧,你真丢脸。 仅当您 100% 确信您的函数不会并且永远不会抛出异常时,才使用此功能。

See this article for details on C++ exception specifications and Microsoft's implementation:

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.

通知家属抬走 2024-07-21 05:56:40

您会发现该版本的 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.

白日梦 2024-07-21 05:56:40

引用自对异常规范的实用看法

(错误)理解

第二个问题与
知道你会得到什么。 尽可能多
知名人士,包括作者
Boost 异常规范
理由,已经说过了,
程序员倾向于使用异常
规范就像他们的行为一样
程序员想要的方式,
而不是他们实际做的方式
表现得好。

这是很多人的想法
异常规范:

  • 保证函数只会抛出列出的异常(可能是
    无)。

  • 根据仅列出的知识启用编译器优化
    例外(可能没有)将是
    抛出。

上述期望再次是,
看似接近正确。

请参阅链接了解完整详细信息。

Quoting from A Pragmatic Look at Exception Specifications:

(Mis)understandings

The second issue has to do with
knowing what you’re getting. As many
notable persons, including the authors
of the Boost exception specification
rationale, have put it,
programmers tend to use exception
specifications as though they behaved
the way the programmer would like,
instead of the way they actually do
behave.

Here’s what many people think that
exception specifications do:

  • Guarantee that functions will only throw listed exceptions (possibly
    none).

  • Enable compiler optimizations based on the knowledge that only listed
    exceptions (possibly none) will be
    thrown.

The above expectations are, again,
deceptively close to being correct.

See the link for the full details.

秋叶绚丽 2024-07-21 05:56:40

抛出异常是不够的,您需要一个 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.

謌踐踏愛綪 2024-07-21 05:56:40

抛出规范的设计有两个目的:

  1. 作为接口实现和接口用户之间的契约 - 您声明可以从您的方法抛出哪些异常,有些人认为它是接口的一部分。 (契约)Ala 在 Java 中检查异常。

  2. 作为一种向编译器发出信号的方式,表明它可以应用某些优化,以防方法/过程不会引发异常(设置异常处理会花费一些费用)

抛出 throw() 子句中未指定的异常是一个错误,但是实现在任何时候都不需要验证为您服务。 事实上,它甚至无法验证,因为它包括子例程调用的子例程中所有可能的异常。 (可能来自其他模块)这甚至在单个模块内都是不可能的,因为很容易减少到停止问题:)

throw specifications are designed for two purposes:

  1. 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.

  2. 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文