C++ 参考手册
- C++11
 - C++14
 - C++17
 - C++20
 - C++ 编译器支持情况表
 - 独立与宿主实现
 - C++ 语言
 - C++ 关键词
 - 预处理器
 - C++ 标准库头文件
 - 具名要求
 - 功能特性测试 (C++20)
 - 工具库
 - 类型支持(基本类型、RTTI、类型特性)
 - 概念库 (C++20)
 - 错误处理
 - 动态内存管理
 - 日期和时间工具
 - 字符串库
 - 容器库
 - 迭代器库
 - 范围库 (C++20)
 - 算法库
 - 数值库
 - 输入/输出库
 - 输入/输出操纵符
 - std::ios_base
 - std::ios_base::failure
 - std::ios_base::event_callback
 - std::ios_base::seekdir
 - std::ios_base::event
 - std::ios_base::fmtflags
 - std::ios_base::iostate
 - std::ios_base::iword
 - std::ios_base::pword
 - std::ios_base::register_callback
 - std::ios_base::sync_with_stdio
 - std::ios_base::Init
 - std::ios_base::openmode
 - std::ios_base::precision
 - std::ios_base::width
 - std::ios_base::imbue
 - std::ios_base::getloc
 - std::ios_base::xalloc
 - std::ios_base::ios_base
 - std::ios_base::~ios_base
 - std::ios_base::flags
 - std::ios_base::setf
 - std::ios_base::unsetf
 - C 风格文件输入/输出
 - std::basic_streambuf
 - std::basic_filebuf
 - std::basic_stringbuf
 - std::strstreambuf
 - std::basic_syncbuf
 - std::basic_ios
 - std::basic_istream
 - std::basic_osyncstream
 - std::basic_ostream
 - std::basic_iostream
 - std::basic_ifstream
 - std::basic_ofstream
 - std::basic_fstream
 - std::basic_istringstream
 - std::basic_ostringstream
 - std::basic_stringstream
 - std::istrstream
 - std::ostrstream
 - std::strstream
 - std::streamoff
 - std::streamsize
 - std::fpos
 - std::iostream_category
 - std::io_errc
 - std::cin, std::wcin
 - std::cout, std::wcout
 - std::cerr, std::wcerr
 - std::clog, std::wclog
 - 注释
 - 文件系统库
 - 本地化库
 - 正则表达式库
 - 原子操作库
 - 线程支持库
 - 实验性 C++ 特性
 - 有用的资源
 - 索引
 - std 符号索引
 - 协程支持 (C++20)
 - C++ 关键词
 
位置:首页 > C++ 参考手册 >输入/输出库 >std::ios_base > std::ios_base::failure
std::ios_base::failure
|   定义于头文件  <ios>
  | 
||
|   class failure;  | 
||
类 std::ios_base::failure 定义输入/输出库中的函数在失败时抛出的异常对象。
std::ios_base::failure 可以定义为 std::ios_base 的成员类,或拥有等价功能的另一个类的同义词( typedef )。 (C++17 起)
| 
 继承图  | 
(C++11 前) | 
| 
 继承图  | 
(C++11 起) | 
成员函数
|     (构造函数)  | 
  构造异常对象  (公开成员函数)  | 
std::ios_base::failure::failure
|   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 );  | 
(C++11 起) | |
以 message 为解释性字符串构造异常对象,能在之后用 what() 取得。
参数
| message | - | 解释性字符串 | 
继承自 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 流的错误码  (枚举)  |