Team LiB   Previous Section   Next Section

13.5 <cerrno>

The <cerrno> header (from the C standard <errno.h> header) declares several macros related to error-handling in the standard library, including the errno object.

EDOM macro Code for a math domain error

int EDOM

Standard library functions that report domain errors set errno to EDOM. A domain error occurs when the domain of an argument is out of range, such as when asking for the square root of a negative number.

The EDOM macro expands to a nonzero integer constant. The value of EDOM is implementation-defined.

EILSEQ macro Code for error in a multibyte character sequence

int EILSEQ

Standard library functions that report errors in multibyte character sequences set errno to EILSEQ. For example, passing an invalid wide character to the wcrtomb function results in EILSEQ.

The EILSEQ macro expands to a nonzero integer constant. The value of EILSEQ is implementation-defined.

Note that EILSEQ is not mentioned in the C++ standard, but because C++ includes the C standard library (Amendment 1), and EILSEQ is part of the C standard, it is part of the C++ standard.

ERANGE macro Code for range error

int ERANGE

Standard library functions that report range errors set errno to ERANGE. A range error occurs when the result of a function is out of range, such as when there is overflow from the pow function.

The ERANGE macro expands to a nonzero integer constant. The value of ERANGE is implementation-defined.

errno macro Global error code object

int& errno

The errno macro expands into an int lvalue. Standard library functions can store an error code in errno and return an error status to the caller. You can also store a value in errno (e.g., resetting the error code to 0).

When a program starts, errno is initially 0. No library function resets errno to 0. Any library function might set errno to a nonzero value, even if it is not documented to do so. Therefore, the only time it is safe to check errno is after a library function returns an error status and is documented to set errno in that case.

figs/acorn.gif

The C++ standard is not explicit as to whether errno is truly a macro (versus a variable). The intent of the standard committee is to define errno as a macro, so do not use std::errno, and if your library declares errno as a variable in the std:: namespace, you can define your own macro:

#define errno (::std::errno)

In a multithreaded environment, a library implementation typically ensures that each thread gets a separate copy of errno. Such considerations fall outside the realm of the C++ standard. Consult your compiler and library documentation for details.

See Also

perror in <cstdio>, strerror in <cstring>

    Team LiB   Previous Section   Next Section