前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则E.2:通过抛出异常来表明函数无法执行指定的任务

C++核心准则E.2:通过抛出异常来表明函数无法执行指定的任务

作者头像
面向对象思考
发布2020-07-28 14:59:15
4230
发布2020-07-28 14:59:15
举报

E.2: Throw an exception to signal that a function can't perform its assigned task

E.2:通过抛出异常来表明函数无法执行指定的任务

Reason(原因)

To make error handling systematic, robust, and non-repetitive.

为了使用错误处理系统化,健壮和不繁琐。

Example(示例)

代码语言:javascript
复制
struct Foo {
    vector<Thing> v;
    File_handle f;
    string s;
};

void use()
{
    Foo bar {{Thing{1}, Thing{2}, Thing{monkey}}, {"my_file", "r"}, "Here we go!"};
    // ...
}

Here, vector and strings constructors may not be able to allocate sufficient memory for their elements, vectors constructor may not be able copy the Things in its initializer list, and File_handle may not be able to open the required file. In each case, they throw an exception for use()'s caller to handle. If use() could handle the failure to construct bar it can take control using try/catch. In either case, Foo's constructor correctly destroys constructed members before passing control to whatever tried to create a Foo. Note that there is no return value that could contain an error code.

这里vector和string的构造函数可能无法为它们的元素分配足够的内存,vector构造函数可能无法复制初始化列表中的内容,File_handle有可能无法打开需要的文件。每种情况中,它们都会向调用者抛出异常以便处理。如果use()可以处理构造bar对象时的错误,它们可以通过try/catry获得控制。其他情况下,Foo的构造函数可以在将控制权交给试图构建Foo的代码之前正确地销毁已经构造完成的成员。注意,代码中没有可以容纳错误码的返回值。

The File_handle constructor might be defined like this:

File_handle的构造函数可能被定义成下面的样子:

代码语言:javascript
复制
File_handle::File_handle(const string& name, const string& mode)
    : f{fopen(name.c_str(), mode.c_str())}
{
    if (!f)
        throw runtime_error{"File_handle: could not open " + name + " as " + mode};
}
Note(注意)

It is often said that exceptions are meant to signal exceptional events and failures. However, that's a bit circular because "what is exceptional?" Examples:

一般情况下会认为异常意味着重大的例外事件和错误。然而,这个问题多少有点绕,到底什么是例外?例如:

  • A precondition that cannot be met
  • 一个前提条件没有满足
  • A constructor that cannot construct an object (failure to establish its class's invariant)
  • 构造函数无法构造对象(无法建立类的不变式)
  • An out-of-range error (e.g., v[v.size()] = 7)
  • 越界错误(例如 v[v.size()]=7)
  • Inability to acquire a resource (e.g., the network is down)
  • 无法获取资源(例如:网络断)

In contrast, termination of an ordinary loop is not exceptional. Unless the loop was meant to be infinite, termination is normal and expected.

相反,结束一个通常的循环不属于异常。只要它不是无限循环,中止就是正常和期待的。

Note(注意)

Don't use a throw as simply an alternative way of returning a value from a function.

不要使用将抛出异常作为从函数中返回结果的另一种方式使用。

Exception(例外)

Some systems, such as hard-real-time systems require a guarantee that an action is taken in a (typically short) constant maximum time known before execution starts. Such systems can use exceptions only if there is tool support for accurately predicting the maximum time to recover from a throw.

有些系统,例如硬实时系统要求保证一个动作在开始执行之前就能确定其执行时间小于某个固定值(通常很小)。这样的系统只有在存在某种可以准确预测系统从抛出异常过程中恢复的最大时间的工具时才可以使用异常。

See also: RAII

参见:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e6-use-raii-to-prevent-leaks

See also: discussion

参见:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#discussion-usage-of-noexcept

Note(注意)

Before deciding that you cannot afford or don't like exception-based error handling, have a look at the alternatives; they have their own complexities and problems. Also, as far as possible, measure before making claims about efficiency.

在决定你无法承担或者不喜欢基于例外的错误处理之前,考虑以下其他选项;它们包含自己的复杂性和问题。同时,只要可能的话,在抱怨效率问题之前进行测量。

原文链接https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e2-throw-an-exception-to-signal-that-a-function-cant-perform-its-assigned-task

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • E.2: Throw an exception to signal that a function can't perform its assigned task
  • Reason(原因)
    • Note(注意)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档