Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Error handling

&pagelevel(3)&pagelevel

In order to program effectively, it is advantageous in the case of most function calls to check whether the function performed successfully. This may be done as follows:

if(fct(...) == error result)    /* Query error return value */
  {
    perror("fct:");             /* Output error information */
    exit(error code);           /* Response to the error, e.g. */
  }                             /* program termination in this case */
else...

Most functions return an error return value when an error occurs.
Furthermore, in many cases, the internal C variable errno (type integer) is set to an appropriate error code. Information specifying the error in more detail is then edited internally (in a structure) on the basis of this error code. The information output by the perror function contains:

  • a brief error text explaining the error,

  • the name of the function at which the error occurred,

  • the DMS error code (hexadecimal), if any, for incorrect file access.

All error codes as well as the associated error information are defined in the include file <errno.h>.

If various types of errors and thus different error codes are possible for a function, it may be useful to query the errno variable for the error code so as to vary the response (if appropriate) to the errors that occur. Each error code is represented by a symbolic constant defined in <errno.h>: ERANGE, for example, indicates an overflow error (value 2).
A query could appear as shown in the following example, which uses the signal function:

#include <errno.h>
       .
       .
if(signal(sig, fct) == 1) /* Query error result */
{
  if(errno == EFAULT)
       .           /* Responses to EFAULT (invalid address) */
       .
  else if(errno == EINVAL)
       .           /* Responses to EINVAL (invalid argument) */
       .
}
else...

In addition to the errno variable there are two other variables defined in the include file <errno.h>:

The name of the errored function can be accessed with __errcmd, and the hexadecimal DMS code with __errhex. Both variables are of char[8] type.

Notes

  • The variables errno, __errcmd and __errhex must not be explicitly defined by the user. The include file <errno.h> must be incorporated in the program in order for these variables to be queried.

  • The contents of the area in which the errno error code is internally stored are preserved until they are overwritten with current information when an error occurs again. perror calls and queries of the errno, __errcmd and __errhex variables are therefore only useful immediately after a function has provided an error return value.

In the examples given for the individual function descriptions, error queries have often been omitted so as to keep the examples from swelling unnecessarily.