std::ios_base:: failure
|
定义于头文件
<ios>
|
||
|
class
failure
;
|
||
类
std::ios_base::failure
定义了输入/输出库中函数在失败时抛出的异常对象。
|
|
(since C++17) |
|
继承关系图 |
(C++11 前) |
|
继承关系图 |
(C++11 起) |
目录 |
成员函数
|
(constructor)
|
构造带有给定消息的新
failure
对象
(公开成员函数) |
|
operator=
|
替换
failure
对象
(公开成员函数) |
|
what
|
返回说明性字符串
(公开成员函数) |
std::ios_base::failure:: failure
| (1) | ||
|
explicit
failure
(
const
std::
string
&
message
)
;
|
(until C++11) | |
|
explicit
failure
(
const
std::
string
&
message,
const std:: error_code & ec = std:: io_errc :: stream ) ; |
(since C++11) | |
|
explicit
failure
(
const
char
*
message,
const std:: error_code & ec = std:: io_errc :: stream ) ; |
(2) | (since C++11) |
| (3) | ||
|
failure
(
const
failure
&
other
)
;
|
(until C++11) | |
|
failure
(
const
failure
&
other
)
noexcept
;
|
(since C++11) | |
std::ios_base::failure
,则满足
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
。
(since C++11)
参数
| message | - | 解释性字符串 |
| ec | - | 用于标识失败具体原因的错误码 |
| other | - |
要拷贝的另一个
failure
对象
|
注解
由于不允许在拷贝
std::ios_base::failure
时抛出异常,该消息通常以单独分配的引用计数字符串形式内部存储。这也是为什么没有接受
std::
string
&&
参数的构造函数的原因:无论如何都需要复制内容。
std::ios_base::failure:: operator=
|
failure
&
operator
=
(
const
failure
&
other
)
;
|
(until C++11) | |
|
failure
&
operator
=
(
const
failure
&
other
)
noexcept
;
|
(since C++11) | |
使用
other
的内容进行赋值。
如果
*
this
和
other
都具有动态类型
std::ios_base::failure
,则赋值后
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
。
(since C++11)
参数
| other | - | 用于赋值的另一个异常对象 |
返回值
* this
std::ios_base::failure:: what
|
virtual
const
char
*
what
(
)
const
throw
(
)
;
|
(until C++11) | |
|
virtual
const
char
*
what
(
)
const
noexcept
;
|
(since C++11) | |
返回解释性字符串。
返回值
指向实现定义的以空字符结尾的解释性字符串的指针。该字符串适合转换并显示为 std::wstring 。该指针保证至少在获取它的异常对象被销毁之前,或者在异常对象上调用非const成员函数(例如拷贝赋值运算符)之前保持有效。
备注
实现允许但不要求重写
what()
。
继承自 std:: system_error
成员函数
|
返回错误码
(
std::system_error
的公开成员函数)
|
|
|
[virtual]
|
返回说明性字符串
(
std::system_error
的虚公开成员函数)
|
继承自 std:: exception
成员函数
|
[virtual]
|
销毁异常对象
(
std::exception
的虚公开成员函数)
|
|
[virtual]
|
返回解释性字符串
(
std::exception
的虚公开成员函数)
|
注释
在
LWG issue 331
解决之前,
std::ios_base::failure
声明了一个不带
throw
(
)
的析构函数,而
std::exception::~exception()
被声明为带有
throw
(
)
[1]
。这意味着
std::ios_base::failure::~failure()
具有较弱的异常规范。解决方案是移除该声明以保持不抛出异常的规范。
LWG issue 363
针对相同的缺陷,其解决方案是在
std::ios_base::failure::~failure()
的声明中添加
throw
(
)
。由于两个解决方案之间存在冲突,该决议最终未被采纳。
示例
#include <fstream> #include <iostream> int main() { std::ifstream f("doesn't exist"); try { f.exceptions(f.failbit); } catch (const std::ios_base::failure& e) { std::cout << "Caught an ios_base::failure.\n" << "Explanatory string: " << e.what() << '\n' << "Error code: " << e.code() << '\n'; } }
可能的输出:
Caught an ios_base::failure. Explanatory string: ios_base::clear: unspecified iostream_category error Error code: iostream:1
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| 问题报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 48 | C++98 |
构造函数重载 (1) 使用
msg
初始化基类
std::exception
但基类没有匹配的构造函数 |
移除对应的
描述说明 |
| LWG 331 | C++98 |
std::ios_base::failure
声明了析构函数但未使用
throw
(
)
异常规范
|
移除析构函数声明 |
参见
|
(C++11)
|
IO流错误码
(枚举) |