温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++怎么使用异常发信号

发布时间:2021-11-25 15:47:35 来源:亿速云 阅读:133 作者:iii 栏目:大数据

这篇文章主要讲解了“C++怎么使用异常发信号”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++怎么使用异常发信号”吧!

如果必要任务失败了,使用异常发信号

Reason(原因)

It should not be possible to ignore an error because that could leave the system or a computation in an undefined (or unexpected) state. This is a major source of errors.

错误可能会让系统或计算处于没有定义(或者意外)的状态,应该杜绝错误被忽略的可能性。忽略对错误的处理是大部分问题的主要原因。

Example(示例)

int printf(const char* ...);    // bad: return negative number if output fails
template <class F, class ...Args>// good: throw system_error if unable to start the new threadexplicit thread(F&& f, Args&&... args);
Note(注意)

What is an error?

什么是错误?

错误意味着功能无法达成它对外宣称的目的(包括建立事后条件)。忽略错误的调用代码可能造成错误的结果或者没有定义的系统状态。例如,无法连接到远程服务器不是它自己造成的错误:服务器可能因为各种原因拒绝连接,因此自然的做法就是返回一个结果让调用者确认。如果无法连接被认为是错误,这个失败应该抛出异常。

Exception(异常)

Many traditional interface functions (e.g., UNIX signal handlers) use error codes (e.g., errno) to report what are really status codes, rather than errors. You don't have a good alternative to using such, so calling these does not violate the rule.

很多传统的函数(例如UNIX信号量处理函数)使用错误码(例如errno)报告实际的状态。由于没有好的选项来代替它们,因此调用这些函数不算违反规则。

Alternative(可选项)

If you can't use exceptions (e.g., because your code is full of old-style raw-pointer use or because there are hard-real-time constraints), consider using a style that returns a pair of values:

如果不能使用异常(例如,因为代码到处都是旧风格的原始指针,或者存在硬实时约束),考虑使用返回结果对的风格。

int val;int error_code;tie(val, error_code) = do_something();if (error_code) {    // ... handle the error or exit ...}
// ... use val ...

译者注:tie是C++引入的新特性,用于解包同样是C++11引入的tuple数据。

This style unfortunately leads to uninitialized variables. Since C++17 the "structured bindings" feature can be used to initialize variables directly from the return value:

这个方式有一个缺点就是会导致未初始化变量。使用C++17之后导入的结构化绑定功能可以通过返回值直接初始化变量。

auto [val, error_code] = do_something();if (error_code) {    // ... handle the error or exit ...}// ... use val ...

Note(注意)

We don't consider "performance" a valid reason not to use exceptions.

我们不认为“性能”是不使用异常的合理原因。

  • Often, explicit error checking and handling consume as much time and space as exception handling.

    通常,清楚的错误检查和处理消耗的时间和空间和异常处理相同(译者注:换个说法,目前很多代码中所谓高效率错误处理实际上是偷工减料)

  • Often, cleaner code yields better performance with exceptions (simplifying the tracing of paths through the program and their optimization).

    通常,整洁的代码使用异常会带来更好的性能(简化跟踪程序执行路径和优化)

  • A good rule for performance critical code is to move checking outside the critical part of the code (checking).

       对于性能敏感的代码来说,将检查部分移到代码的关键部分之外是一个好方法。

  • In the longer term, more regular code gets better optimized.

    长期来说,有规律的代码可以被更好地优化。

  • Always carefully measure before making performance claims.

    在宣称性能问题之前一定要仔细地考量。(译者注:例如真的会有性能问题么?真的那么重要么?)

Enforcement(实施建议)

  • (Not enforceable) This is a philosophical guideline that is infeasible to check directly.

    (非强制)这是一条事关编程哲学的准则,无法直接检查。

  • Look for errno.

    选择errno。

感谢各位的阅读,以上就是“C++怎么使用异常发信号”的内容了,经过本文的学习后,相信大家对C++怎么使用异常发信号这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI