std::ios_base::failure
来自cppreference.com
| 定义于头文件 <ios>
|
||
| class failure; |
||
类 std::ios_base::failure 定义输入/输出库中的函数在失败时抛出的异常对象。
|
std::ios_base::failure 可以定义为 std::ios_base 的成员类,或拥有等价功能的另一个类的同义词( typedef )。 |
(C++17 起) |
|
继承图 |
(C++11 前) |
|
继承图 |
(C++11 起) |
成员函数
| (构造函数) |
构造拥有指定消息的新 failure 对象 (公开成员函数) |
| operator= |
替换 failure 对象 (公开成员函数) |
| what |
返回解释字符串 (公开成员函数) |
std::ios_base::failure::failure
| (1) | ||
| explicit failure( const std::string& message ); |
(C++11 前) | |
| explicit failure( const std::string& message, const std::error_code& ec = std::io_errc::stream ); |
(C++11 起) | |
| explicit failure( const char* message, const std::error_code& ec = std::io_errc::stream ); |
(2) | (C++11 起) |
| (3) | ||
| failure( const failure& other ); |
(C++11 前) | |
| failure( const failure& other ) noexcept; |
(C++11 起) | |
3) 复制构造函数。以
other 的内容初始化内容。若 *this 与 other 均拥有动态类型 std::ios_base::failure 则 std::strcmp(what(), other.what()) == 0 。 (C++11 起)参数
| message | - | 解释性字符串 |
| ec | - | 鉴别特定失败理由的错误码 |
| other | - | 要复制的另一 failure
|
注解
因为不容许复制 std::ios_base::failure 抛出异常,通常将此消息在内部存储为分离分配的引用计数字符串。这也是构造函数不接收 std::string&& 参数的理由:无论如何它必须复制内容。
std::ios_base::failure::operator=
| failure& operator=( const failure& other ); |
(C++11 前) | |
| failure& operator=( const failure& other ) noexcept; |
(C++11 起) | |
以 other 的内容赋值。若 *this 与 other 均拥有动态类型 std::ios_base::failure 则赋值后 std::strcmp(what(), other.what()) == 0 。 (C++11 起)
参数
| other | - | 用以赋值的另一异常对象 |
返回值
*this
std::ios_base::failure::what
| virtual const char* what() const throw(); |
(C++11 前) | |
| virtual const char* what() const noexcept; |
(C++11 起) | |
返回解释字符串。
参数
(无)
返回值
指向有解释信息的空终止字符串的指针。该字符串适合转换并显示为 std::wstring 。保证该指针至少到获得它来源的异常对象被销毁,或在该异常对象上调用非 const 成员函数(例如复制赋值运算符)为止合法。
注解
允许但不要求实现覆写 what() 。
继承自 std::system_error
成员函数
| 返回错误码 ( std::system_error 的公开成员函数) | |
| [虚] |
返回解释性字符串 ( std::system_error 的虚公开成员函数) |
继承自 std::exception
成员函数
| [虚] |
析构该异常对象 ( std::exception 的虚公开成员函数) |
| [虚] |
返回解释性字符串 ( std::exception 的虚公开成员函数) |
示例
运行此代码
#include <iostream> #include <fstream> 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++11) |
IO 流的错误码 (枚举) |