加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何正确地抛出一个不仅仅是一个构造函数的异常?

发布时间:2020-12-16 05:28:32 所属栏目:百科 来源:网络整理
导读:我有一个异常类,我想在其上放置更多的信息.我可以创建Exception对象,调用其中的一些函数,然后将其抛出,而不会复制它的任何副本? 我发现唯一的方法是抛出一个指向该对象的指针: class Exception : public std::runtime_error{public: Exception(const std::
我有一个异常类,我想在其上放置更多的信息.我可以创建Exception对象,调用其中的一些函数,然后将其抛出,而不会复制它的任何副本?

我发现唯一的方法是抛出一个指向该对象的指针:

class Exception : public std::runtime_error
{
public:
    Exception(const std::string& msg) : std::runtime_error(msg) {}
    void set_line(int line) {line_ = line;}
    int get_line() const {return line_;}
private:
    int line_ = 0;
};

std::unique_ptr<Exception> e(new Exception("message"));
e->set_line(__LINE__);
throw e;
...
catch (std::unique_ptr<Exception>& e) {...}

但是通常可以避免指针抛出异常,还有其他方法吗?

还可以通过构造函数设置所有选项,但是如果将更多的字段添加到类中,并且要对要设置的字段进行细粒度控制,则可以快速变为不可扩展:

throw Exception("message"); // or:
throw Exception("message",__LINE__); // or:
throw Exception("message",__FILE__); // or:
throw Exception("message",__LINE__,__FILE__); // etc.

解决方法

使用std :: move怎么办?
Exception e("message");
e.set_line(__LINE__);
throw std::move(e);

或者,您可以创建如下的Java-esque构建器:

class ExceptionBuilder;

class Exception : public std::runtime_error
{
public:
    static ExceptionBuilder create(const std::string &msg);

    int get_line() const {return line_;}
    const std::string& get_file() const { return file_; }
private:
    // Constructor is private so that the builder must be used.
    Exception(const std::string& msg) : std::runtime_error(msg) {}

    int line_ = 0;
    std::string file_;

    // Give builder class access to the exception internals.
    friend class ExceptionBuilder;
};

// Exception builder.
class ExceptionBuilder
{
public:
    ExceptionBuilder& with_line(const int line) { e_.line_ = line; return *this; }
    ExceptionBuilder& with_file(const std::string &file) { e_.file_ = file; return *this; }
    Exception finalize() { return std::move(e_); }
private:
    // Make constructors private so that ExceptionBuilder cannot be instantiated by the user.
    ExceptionBuilder(const std::string& msg) : e_(msg) { }
    ExceptionBuilder(const ExceptionBuilder &) = default;
    ExceptionBuilder(ExceptionBuilder &&) = default;

    // Exception class can create ExceptionBuilders.
    friend class Exception;

    Exception e_;
};

inline ExceptionBuilder Exception::create(const std::string &msg)
{
    return ExceptionBuilder(msg);
}

这样使用:

throw Exception::create("TEST")
    .with_line(__LINE__)
    .with_file(__FILE__)
    .finalize();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读