cppreference的std::errc: std::errc - cppreference.com
对应定义(come from: 2022\Community\VC\Tools\MSVC\14.33.31629\include\xerrc.h)
enum class errc { // names for generic error codesaddress_family_not_supported = 102, // EAFNOSUPPORTaddress_in_use = 100, // EADDRINUSEaddress_not_available = 101, // EADDRNOTAVAILalready_connected = 113, // EISCONNargument_list_too_long = 7, // E2BIGargument_out_of_domain = 33, // EDOMbad_address = 14, // EFAULTbad_file_descriptor = 9, // EBADFbad_message = 104, // EBADMSGbroken_pipe = 32, // EPIPEconnection_aborted = 106, // ECONNABORTEDconnection_already_in_progress = 103, // EALREADYconnection_refused = 107, // ECONNREFUSEDconnection_reset = 108, // ECONNRESETcross_device_link = 18, // EXDEVdestination_address_required = 109, // EDESTADDRREQdevice_or_resource_busy = 16, // EBUSYdirectory_not_empty = 41, // ENOTEMPTYexecutable_format_error = 8, // ENOEXECfile_exists = 17, // EEXISTfile_too_large = 27, // EFBIGfilename_too_long = 38, // ENAMETOOLONGfunction_not_supported = 40, // ENOSYShost_unreachable = 110, // EHOSTUNREACHidentifier_removed = 111, // EIDRMillegal_byte_sequence = 42, // EILSEQinappropriate_io_control_operation = 25, // ENOTTYinterrupted = 4, // EINTRinvalid_argument = 22, // EINVALinvalid_seek = 29, // ESPIPEio_error = 5, // EIOis_a_directory = 21, // EISDIRmessage_size = 115, // EMSGSIZEnetwork_down = 116, // ENETDOWNnetwork_reset = 117, // ENETRESETnetwork_unreachable = 118, // ENETUNREACHno_buffer_space = 119, // ENOBUFSno_child_process = 10, // ECHILDno_link = 121, // ENOLINKno_lock_available = 39, // ENOLCKno_message_available = 120, // ENODATAno_message = 122, // ENOMSGno_protocol_option = 123, // ENOPROTOOPTno_space_on_device = 28, // ENOSPCno_stream_resources = 124, // ENOSRno_such_device_or_address = 6, // ENXIOno_such_device = 19, // ENODEVno_such_file_or_directory = 2, // ENOENTno_such_process = 3, // ESRCHnot_a_directory = 20, // ENOTDIRnot_a_socket = 128, // ENOTSOCKnot_a_stream = 125, // ENOSTRnot_connected = 126, // ENOTCONNnot_enough_memory = 12, // ENOMEMnot_supported = 129, // ENOTSUPoperation_canceled = 105, // ECANCELEDoperation_in_progress = 112, // EINPROGRESSoperation_not_permitted = 1, // EPERMoperation_not_supported = 130, // EOPNOTSUPPoperation_would_block = 140, // EWOULDBLOCKowner_dead = 133, // EOWNERDEADpermission_denied = 13, // EACCESprotocol_error = 134, // EPROTOprotocol_not_supported = 135, // EPROTONOSUPPORTread_only_file_system = 30, // EROFSresource_deadlock_would_occur = 36, // EDEADLKresource_unavailable_try_again = 11, // EAGAINresult_out_of_range = 34, // ERANGEstate_not_recoverable = 127, // ENOTRECOVERABLEstream_timeout = 137, // ETIMEtext_file_busy = 139, // ETXTBSYtimed_out = 138, // ETIMEDOUTtoo_many_files_open_in_system = 23, // ENFILEtoo_many_files_open = 24, // EMFILEtoo_many_links = 31, // EMLINKtoo_many_symbolic_link_levels = 114, // ELOOPvalue_too_large = 132, // EOVERFLOWwrong_protocol_type = 136 // EPROTOTYPE
};
示例代码:
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-threadtry{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");}}
}
运行程序之后的输出:
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