errno

From cppreference.com
 
 
 
Error handling
Exception handling
exception
uncaught_exception
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 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
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)
 
Defined in header <cerrno>
#define errno /*implementation-defined*/

errno is a preprocessor macro that expands to a thread-local modifiable lvalue of type int. Several standard library functions indicate errors by writing positive integers to errno. Typically, the value of errno is set to one the error codes, listed in <cerrno> as macro constants that begin with the letter E, followed by uppercase letters or digits.

The value of errno is 0 at program startup, and although library functions are allowed to write positive integers to errno whether or not an error occurred, library functions never store 0 in errno.

[edit] Example

#include <iostream>
#include <cmath>
#include <cerrno>
#include <cstring>
 
int main()
{
    double not_a_number = std::log(-1.0);
    if (errno == EDOM) {
        std::cout << "log(-1) failed: " << std::strerror(errno) << '\n';
    }
}

Output:

log(-1) failed: Numerical argument out of domain

[edit] See also

macros for standard POSIX-compatible error conditions
(macro constant)
displays a character string corresponding of the current error to stderr
(function)
returns a text version of a given error code
(function)