std::domain_error
来自 cppreference.cn
定义于头文件 <stdexcept> |
||
class domain_error; |
||
定义要抛出为异常的对象类型。实现可能使用它来报告域错误,即输入在操作定义的域之外的情况。
标准库组件不抛出此异常(数学函数按照 math_errhandling 中的规定报告域错误)。但是,第三方库会使用它。例如,如果启用了 boost::math::policies::throw_on_error
(默认设置),boost.math 会抛出 std::domain_error
。
继承关系图
内容 |
[编辑] 成员函数
(构造函数) |
构造一个新的 domain_error 对象,并带有给定的消息(公有成员函数) |
operator= |
替换 domain_error 对象(公有成员函数) |
std::domain_error::domain_error
domain_error( const std::string& what_arg ); |
(1) | |
domain_error( const char* what_arg ); |
(2) | |
domain_error( const domain_error& other ); |
(3) | (C++11 起 noexcept) |
3) 复制构造函数。如果 *this 和 other 都具有动态类型
std::domain_error
,则 std::strcmp(what(), other.what()) == 0。复制构造函数不会抛出任何异常。参数
what_arg | - | 解释性字符串 |
other | - | 要复制的另一个异常对象 |
异常
1,2) 可能抛出 std::bad_alloc。
注释
由于不允许复制 std::domain_error
时抛出异常,因此此消息通常在内部存储为单独分配的引用计数字符串。这也是为什么没有接受 std::string&&
的构造函数的原因:无论如何它都必须复制内容。
在 LWG issue 254 解决之前,非复制构造函数只能接受 std::string。为了构造 std::string 对象,动态分配是强制性的。
在 LWG issue 471 解决之后,派生的标准异常类必须具有公开可访问的复制构造函数。只要原始对象和复制对象通过 what()
获取的解释性字符串相同,就可以隐式定义它。
std::domain_error::operator=
domain_error& operator=( const domain_error& other ); |
(C++11 起 noexcept) | |
将内容赋值为 other 的内容。如果 *this 和 other 都具有动态类型 std::domain_error
,则赋值后 std::strcmp(what(), other.what()) == 0。复制赋值运算符不会抛出任何异常。
参数
other | - | 要赋值的另一个异常对象 |
返回值
*this
注释
在 LWG issue 471 解决之后,派生的标准异常类必须具有公开可访问的复制赋值运算符。只要原始对象和复制对象通过 what()
获取的解释性字符串相同,就可以隐式定义它。
继承自 std::logic_error
继承自 std::exception
成员函数
[虚函数] |
销毁异常对象 ( std::exception 的虚公有成员函数) |
[虚函数] |
返回解释性字符串 ( std::exception 的虚公有成员函数) |
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 254 | C++98 | 接受 const char* 的构造函数缺失 | 已添加 |
LWG 471 | C++98 | std::domain_error 的解释性字符串副本是实现定义的 |
它们与 原始 std::domain_error 对象相同 |