命名空间
变体
操作

std::iostream_category

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

获取 iostream 错误的静态错误类别对象的引用。该对象需要重写虚函数 error_category::name() 以返回指向字符串 "iostream" 的指针。它用于识别类型为 std::ios_base::failure 的异常中提供的错误代码。

目录

[编辑] 参数

(无)

[编辑] 返回值

对静态对象的引用,该静态对象的运行时类型未指定,派生自 std::error_category

[编辑] 示例

#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"
                  << "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

[编辑] 缺陷报告

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

缺陷报告 应用于 发布时的行为 正确的行为
LWG 2087 C++11 iostream_category 未声明为 noexcept 声明为 noexcept

[编辑] 另请参阅

流异常
(std::ios_base 的公共成员类) [编辑]
(C++11)
I/O 流错误码
(枚举) [编辑]