c++标准库的错误代码

cppreference的std::errc: std::errc - cppreference.com

对应定义(come from: 2022\Community\VC\Tools\MSVC\14.33.31629\include\xerrc.h)

cpp 复制代码
enum class errc { // names for generic error codes
    address_family_not_supported       = 102, // EAFNOSUPPORT
    address_in_use                     = 100, // EADDRINUSE
    address_not_available              = 101, // EADDRNOTAVAIL
    already_connected                  = 113, // EISCONN
    argument_list_too_long             = 7, // E2BIG
    argument_out_of_domain             = 33, // EDOM
    bad_address                        = 14, // EFAULT
    bad_file_descriptor                = 9, // EBADF
    bad_message                        = 104, // EBADMSG
    broken_pipe                        = 32, // EPIPE
    connection_aborted                 = 106, // ECONNABORTED
    connection_already_in_progress     = 103, // EALREADY
    connection_refused                 = 107, // ECONNREFUSED
    connection_reset                   = 108, // ECONNRESET
    cross_device_link                  = 18, // EXDEV
    destination_address_required       = 109, // EDESTADDRREQ
    device_or_resource_busy            = 16, // EBUSY
    directory_not_empty                = 41, // ENOTEMPTY
    executable_format_error            = 8, // ENOEXEC
    file_exists                        = 17, // EEXIST
    file_too_large                     = 27, // EFBIG
    filename_too_long                  = 38, // ENAMETOOLONG
    function_not_supported             = 40, // ENOSYS
    host_unreachable                   = 110, // EHOSTUNREACH
    identifier_removed                 = 111, // EIDRM
    illegal_byte_sequence              = 42, // EILSEQ
    inappropriate_io_control_operation = 25, // ENOTTY
    interrupted                        = 4, // EINTR
    invalid_argument                   = 22, // EINVAL
    invalid_seek                       = 29, // ESPIPE
    io_error                           = 5, // EIO
    is_a_directory                     = 21, // EISDIR
    message_size                       = 115, // EMSGSIZE
    network_down                       = 116, // ENETDOWN
    network_reset                      = 117, // ENETRESET
    network_unreachable                = 118, // ENETUNREACH
    no_buffer_space                    = 119, // ENOBUFS
    no_child_process                   = 10, // ECHILD
    no_link                            = 121, // ENOLINK
    no_lock_available                  = 39, // ENOLCK
    no_message_available               = 120, // ENODATA
    no_message                         = 122, // ENOMSG
    no_protocol_option                 = 123, // ENOPROTOOPT
    no_space_on_device                 = 28, // ENOSPC
    no_stream_resources                = 124, // ENOSR
    no_such_device_or_address          = 6, // ENXIO
    no_such_device                     = 19, // ENODEV
    no_such_file_or_directory          = 2, // ENOENT
    no_such_process                    = 3, // ESRCH
    not_a_directory                    = 20, // ENOTDIR
    not_a_socket                       = 128, // ENOTSOCK
    not_a_stream                       = 125, // ENOSTR
    not_connected                      = 126, // ENOTCONN
    not_enough_memory                  = 12, // ENOMEM
    not_supported                      = 129, // ENOTSUP
    operation_canceled                 = 105, // ECANCELED
    operation_in_progress              = 112, // EINPROGRESS
    operation_not_permitted            = 1, // EPERM
    operation_not_supported            = 130, // EOPNOTSUPP
    operation_would_block              = 140, // EWOULDBLOCK
    owner_dead                         = 133, // EOWNERDEAD
    permission_denied                  = 13, // EACCES
    protocol_error                     = 134, // EPROTO
    protocol_not_supported             = 135, // EPROTONOSUPPORT
    read_only_file_system              = 30, // EROFS
    resource_deadlock_would_occur      = 36, // EDEADLK
    resource_unavailable_try_again     = 11, // EAGAIN
    result_out_of_range                = 34, // ERANGE
    state_not_recoverable              = 127, // ENOTRECOVERABLE
    stream_timeout                     = 137, // ETIME
    text_file_busy                     = 139, // ETXTBSY
    timed_out                          = 138, // ETIMEDOUT
    too_many_files_open_in_system      = 23, // ENFILE
    too_many_files_open                = 24, // EMFILE
    too_many_links                     = 31, // EMLINK
    too_many_symbolic_link_levels      = 114, // ELOOP
    value_too_large                    = 132, // EOVERFLOW
    wrong_protocol_type                = 136 // EPROTOTYPE
};

示例代码:

cpp 复制代码
void print_error(const std::string& details, std::error_code error_code)
{
    std::string value_name;
    if (error_code == std::errc::invalid_argument)
        value_name = "std::errc::invalid_argument";
    if (error_code == std::errc::no_such_file_or_directory)
        value_name = "std::errc::no_such_file_or_directory";
    if (error_code == std::errc::is_a_directory)
        value_name = "std::errc::is_a_directory";
    if (error_code == std::errc::permission_denied)
        value_name = "std::errc::permission_denied";

    std::cout << details << ":\n  "
              << std::quoted(error_code.message())
              << " (" << value_name << "), " << error_code
              << " \n";
}

void print_errno(const std::string& details, int errno_value = errno)
{
    print_error(details, std::make_error_code(std::errc(errno_value)));
}

void testMain()
{
    // Detaching a not-a-thread
    try
    {
        std::thread().detach();
    }
    catch (const std::system_error& e)
    {
        print_error("Error detaching empty thread", e.code());
    }

    // Opening nonexistent file
    {
        std::ifstream nofile("nonexistent-file");
        if (!nofile.is_open())
        {
            print_errno("Error opening nonexistent file for reading");
        }
    }

    // Reading from directory as a file
    {
        std::filesystem::create_directory("testDir");
        std::ifstream         dir_stream("testDir");
        [[maybe_unused]] char c = dir_stream.get();
        if (!dir_stream.good())
        {
            print_errno("Error reading data from directory");
        }
    }

    // Open readonly file for writing
    {
        {
            std::fstream new_file("readonly-file", std::ios::out);
        }
        std::filesystem::permissions("readonly-file", std::filesystem::perms::owner_read);
        std::fstream write_readonly("readonly-file", std::ios::out);
        if (!write_readonly.is_open())
        {
            print_errno("Error opening readonly file for writing");
        }
    }
}

运行程序之后的输出:

cpp 复制代码
Error detaching empty thread:
  "invalid argument" (std::errc::invalid_argument), generic:22
Error opening nonexistent file for reading:
  "no such file or directory" (std::errc::no_such_file_or_directory), generic:2
Error reading data from directory:
  "permission denied" (std::errc::permission_denied), generic:13
Error opening readonly file for writing:
  "permission denied" (std::errc::permission_denied), generic:13
相关推荐
小明同学0111 小时前
C++后端项目:统一大模型接入 SDK(四)
服务器·开发语言·c++·计算机网络·chatgpt
安妮的小熊呢11 小时前
CRMEB开源商城系统 & 标准版系统(PHP)开发规范
开发语言·javascript·php
子榆.11 小时前
CANN ATC编译器:模型从Python到达芬奇指令走了多远
开发语言·python·neo4j
Dontla12 小时前
Multi-Agent多智能体项目如何从MVP过渡到生产项目?
开发语言
兰令水12 小时前
topcode【随机算法题】【2026.5.20打卡-java版本】
java·开发语言·算法
我还记得那天12 小时前
C语言递归实现汉诺塔问题
c语言·开发语言
不吃土豆的马铃薯12 小时前
Spdlog 入门:日志记录器与日志槽基础详解
服务器·开发语言·c++·c·日志·spdlog
此生决int12 小时前
算法从入门到精通——前缀和
c++·算法·蓝桥杯
凯瑟琳.奥古斯特12 小时前
传输层核心功能解析
开发语言·网络·职场和发展
Fuyo_111912 小时前
C++中的活字印刷术——模板·初阶
开发语言·c++·笔记