• c++标准库的错误代码


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

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

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

    示例代码:

    1. void print_error(const std::string& details, std::error_code error_code)
    2. {
    3. std::string value_name;
    4. if (error_code == std::errc::invalid_argument)
    5. value_name = "std::errc::invalid_argument";
    6. if (error_code == std::errc::no_such_file_or_directory)
    7. value_name = "std::errc::no_such_file_or_directory";
    8. if (error_code == std::errc::is_a_directory)
    9. value_name = "std::errc::is_a_directory";
    10. if (error_code == std::errc::permission_denied)
    11. value_name = "std::errc::permission_denied";
    12. std::cout << details << ":\n "
    13. << std::quoted(error_code.message())
    14. << " (" << value_name << "), " << error_code
    15. << " \n";
    16. }
    17. void print_errno(const std::string& details, int errno_value = errno)
    18. {
    19. print_error(details, std::make_error_code(std::errc(errno_value)));
    20. }
    21. void testMain()
    22. {
    23. // Detaching a not-a-thread
    24. try
    25. {
    26. std::thread().detach();
    27. }
    28. catch (const std::system_error& e)
    29. {
    30. print_error("Error detaching empty thread", e.code());
    31. }
    32. // Opening nonexistent file
    33. {
    34. std::ifstream nofile("nonexistent-file");
    35. if (!nofile.is_open())
    36. {
    37. print_errno("Error opening nonexistent file for reading");
    38. }
    39. }
    40. // Reading from directory as a file
    41. {
    42. std::filesystem::create_directory("testDir");
    43. std::ifstream dir_stream("testDir");
    44. [[maybe_unused]] char c = dir_stream.get();
    45. if (!dir_stream.good())
    46. {
    47. print_errno("Error reading data from directory");
    48. }
    49. }
    50. // Open readonly file for writing
    51. {
    52. {
    53. std::fstream new_file("readonly-file", std::ios::out);
    54. }
    55. std::filesystem::permissions("readonly-file", std::filesystem::perms::owner_read);
    56. std::fstream write_readonly("readonly-file", std::ios::out);
    57. if (!write_readonly.is_open())
    58. {
    59. print_errno("Error opening readonly file for writing");
    60. }
    61. }
    62. }

    运行程序之后的输出:

    1. Error detaching empty thread:
    2. "invalid argument" (std::errc::invalid_argument), generic:22
    3. Error opening nonexistent file for reading:
    4. "no such file or directory" (std::errc::no_such_file_or_directory), generic:2
    5. Error reading data from directory:
    6. "permission denied" (std::errc::permission_denied), generic:13
    7. Error opening readonly file for writing:
    8. "permission denied" (std::errc::permission_denied), generic:13

  • 相关阅读:
    DateTimeUtils 日期时间相关的工具类
    基于Django+MySQL的智慧校园系统
    Cobalt Strike-修改默认证书、混淆流量-教程
    一次springboot和redis缓存的实践
    PCIe(二)——TLP包构成
    新考纲下的PMP考试有多难?
    前端体系中哪些发起异步请求的工具?
    【华为机试真题 JAVA】字符统计及重排-100
    SpringBoot-application.yml多环境配置
    python分析inkscape路径数据的简单方案
  • 原文地址:https://blog.csdn.net/vily_lei/article/details/132804217