如何正确抛出一个需要的不仅仅是构造函数的异常
How to properly throw an exception which needs more than just a constructor?
>我有一个异常类,我想在抛出它之前设置更多信息。我可以创建 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.
C++异常类应该是可复制的或至少是可移动的。在您的示例中,使类可复制是添加默认复制构造函数的问题:
Exception(Exception const&) = default;
如果需要在异常类中封装一些不可复制和不可移动的状态,请将此类状态包装到 std::shared_ptr 中。
您可以创建一个数据保存类,例如 ExceptionData
。然后创建ExceptionData
对象并调用它的方法。然后在 ctor 中使用std::move
创建Exception
对象,如下所示:
ExceptionData data;
data.method();
throw Exception(std::move(data));
当然,ExceptionData
必须是可移动的,并且您必须具有接受ExceptionData &&
的ctor(右值引用(。
如果你真的需要避免复制,它会起作用,但对我来说,这感觉就像是初步优化。想想你的应用中抛出异常的频率,以及让事情复杂化真的值得吗?
使用 std::move 怎么样?
Exception e("message");
e.set_line(__LINE__);
throw std::move(e);
或者,您可以创建一个 Java 式构建器,如下所示:
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();
相关文章:
- 为什么这个案例陈述需要一个"if else"而不仅仅是一个"if"?
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 为什么C++总是显示十六进制内存地址,而不仅仅是整数?
- 为什么我们要做"INT_MAX/10"而不仅仅是INT_MAX?
- 为什么 std::vector 需要 is_trivial 才能按位移动,而不仅仅是is_trivially_copya
- 为什么在这个C++问题中使用const_iterator而不仅仅是迭代器?
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 如果要求比较器是严格的总排序,而不仅仅是严格的弱排序,C++标准算法会更快吗?
- 为什么我们必须在 C++ 中释放数组数组,而不仅仅是整个"matrix"
- 十进制文字不仅仅是LONG_LONG_MAX
- 为什么非放置"新建"和"删除"内置于语言中,而不仅仅是常规函数?
- 使用 fstream 在空格分隔文件中查看下一项(不仅仅是一个字符)的方法
- 这是循环击中两个输出,而不仅仅是我期望的输出
- 在C++中,是否有任何理由生成并立即加入线程,而不仅仅是直接调用函数?
- 如何正确抛出一个需要的不仅仅是构造函数的异常
- 为什么 cppreference 将type_traits xxx_v快捷方式定义为内联 constexpr,而不仅仅是
- C++Qt创建者,我可以评论每一行,而不仅仅是评论选择
- 为什么BOOST.PROGROGROGOTIONS不仅仅是标头
- 使用FP:快速导致错误的VC 结果(不仅仅是不准确)结果 - 这是编译器错误
- 提升系统 1.69.0 不仅仅是标题