命名空间
变体
操作

std::domain_error

来自 cppreference.cn
< cpp‎ | 错误
定义于头文件 <stdexcept>
class domain_error;

定义一个作为异常抛出的对象类型。它可由实现用于报告域错误,即输入超出操作所定义域的情况。

标准库组件不抛出此异常(数学函数按照 math_errhandling 中的规定报告域错误)。然而,第三方库会使用此异常。例如,如果启用了 boost::math::policies::throw_on_error(默认设置),boost.math 会抛出 std::domain_error

cpp/error/exceptioncpp/error/logic errorstd-domain error-inheritance.svg

继承图

目录

[编辑] 成员函数

(构造函数)
使用给定消息构造新的 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 起无异常抛出)
1) 使用 what_arg 作为解释性字符串构造异常对象。构造后,std::strcmp(what(), what_arg.c_str()) == 0
2) 使用 what_arg 作为解释性字符串构造异常对象。构造后,std::strcmp(what(), what_arg) == 0
3) 复制构造函数。如果 *thisother 都具有动态类型 std::domain_error,则 std::strcmp(what(), other.what()) == 0。复制构造函数不能抛出异常。

参数

what_arg - 解释性字符串
其他 - 要拷贝的另一个异常对象

异常

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 起无异常抛出)

将内容赋值为 other 的内容。如果 *thisother 都具有动态类型 std::domain_error,则赋值后 std::strcmp(what(), other.what()) == 0。复制赋值运算符不能抛出异常。

参数

其他 - 用于赋值的另一个异常对象

返回值

*this

注意

LWG issue 471 解决之后,派生标准异常类必须有一个公开可访问的复制赋值运算符。只要通过 what() 获取的解释性字符串对于原始对象和复制对象相同,就可以隐式定义它。

继承自 std::logic_error

继承自 std::exception

成员函数

销毁异常对象
(std::exception 的虚公共成员函数) [编辑]
[virtual]
返回解释字符串
(std::exception 的虚公共成员函数) [编辑]

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 254 C++98 缺少接受 const char* 的构造函数 已添加
LWG 471 C++98 std::domain_error 的解释性字符串
解释性字符串是实现定义的
它们与原始 std::runtime_error 对象的
原始 std::domain_error 对象