CppDS.com

C++ 98 11 14 17 20 手册

std::iostream_category

来自cppreference.com
< cpp‎ | io
定义于头文件 <ios>
const std::error_category& iostream_category() noexcept;
(C++11 起)

获得到为 iostream 错误而设的静态 error_category 对象的引用。要求对象覆写虚函数 error_category::name() 以返回指向字符串 "iostream" 的指针。它亦用于鉴别提供于 std::ios_base::failure 类型异常的 error_code 。

参数

(无)

返回值

获得到导出自 std::error_category 的未指定运行时类型的静态对象的引用。

参阅

#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"
                  << "Error code: " << e.code().value() 
                  << " (" << e.code().message() << ")\n"
                  << "Error category: " << e.code().category().name() << '\n';
 
    }
}

可能的输出:

Caught an ios_base::failure.
Error code: 1 (unspecified iostream_category error)
Error category: iostream

参阅

流异常
(std::ios_base 的公开成员类)
(C++11)
IO 流的错误码
(枚举)
关闭