Print Errno Error
Contents |
Description Since so many functions return -1 on what is "errno" error and set the value of the variable perror errno errno to be some number, it would sure be nice if you strerror(errno) could easily print that in a form that made sense to you. Mercifully, perror() does that. If you want
Perror Example
more description to be printed before the error, you can point the parameter s to it (or you can leave s as NULL and nothing additional will be printed.) In a nutshell, this function takes errno values, like errno undeclared ECONNRESET, and prints them nicely, like "Connection reset by peer." The function strerror() is very similar to perror(), except it returns a pointer to the error message string for a given value (you usually pass in the variable errno.) Return Value strerror() returns a pointer to the error message string. Example int s; s = socket(PF_INET, SOCK_STREAM, 0); if (s == -1) { // some error has occurred // prints "socket error: " + the error message: perror("socket error"); } // similarly: if (listen(s, 10) == -1) { // this prints "an error: " + the error message from errno: printf("an error: %s\n", strerror(errno)); } See Also errno <
(see feature_test_macros(7)): sys_errlist, sys_nerr: _BSD_SOURCE Description The routine perror() produces a message on the standard error output, describing the last error encountered during a call to a describe iterative and concurrent server ? system or library function. First (if s is not NULL and *s
What Is Errno In Unix
is not a null byte ('\0')) the argument string s is printed, followed by a colon and a blank.
Errno Bad Address
Then the message and a new-line. To be of most use, the argument string should include the name of the function that incurred the error. The error number is taken http://beej.us/guide/bgnet/output/html/multipage/perrorman.html from the external variable errno, which is set when errors occur but not cleared when successful calls are made. The global error list sys_errlist[] indexed by errno can be used to obtain the error message without the newline. The largest message number provided in the table is sys_nerr-1. Be careful when directly accessing this list because new error values may not have https://linux.die.net/man/3/perror been added to sys_errlist[]. The use of sys_errlist[] is nowadays deprecated. When a system call fails, it usually returns -1 and sets the variable errno to a value describing what went wrong. (These values can be found in
Containers library Algorithms library Iterators library Numerics library Input/output library Localizations library Regular expressions http://en.cppreference.com/w/cpp/error/errno library (C++11) Atomic operations library (C++11) Thread support library (C++11) Filesystem library (C++17) Technical Specifications [edit] Utilities library Type support (basic types, RTTI, type traits) Dynamic memory management Error handling Program utilities Variadic functions Date and time Function objects initializer_list(C++11) bitset hash(C++11) Relational operators rel_ops::operator!=rel_ops::operator>rel_ops::operator<=rel_ops::operator>= optional, what is any and variant (C++17) optional any variant in_placein_place_tin_place_type_tin_place_index_t Pairs and tuples pair tuple(C++11) apply(C++17) make_from_tuple(C++17) piecewise_construct_t(C++11) piecewise_construct(C++11) integer_sequence(C++14) Swap, forward and move swap exchange(C++14) forward(C++11) move(C++11) move_if_noexcept(C++11) Type operations declval(C++11) as_const(C++17) [edit] Error handling Exception handling exception uncaught_exceptionuncaught_exceptions(C++17) exception_ptr(C++11) make_exception_ptr(C++11) current_exception(C++11) rethrow_exception(C++11) nested_exception(C++11) throw_with_nested(C++11) rethrow_if_nested(C++11) Exception print errno error handling failures terminate terminate_handler get_terminate(C++11) set_terminate unexpected(deprecated) bad_exception unexpected_handler(deprecated) get_unexpected(C++11)(deprecated) set_unexpected(deprecated) Exception categories logic_error invalid_argument domain_error length_error out_of_range runtime_error range_error overflow_error underflow_error tx_exception(TM TS) Error codes Error codes errno Assertions assert system_error facility error_category(C++11) generic_category(C++11) system_category(C++11) error_condition(C++11) errc(C++11) error_code(C++11) system_error(C++11) [edit] Defined in header