Go to the first, previous, next, last section, table of contents.

Error Reporting

The library follows the thread-safe error reporting conventions of the POSIX Threads library. Functions return a non-zero error code to indicate an error and 0 to indicate success.

int status = gsl_function(...)

if (status) { /* an error occurred */
  .....       
  /* status value specifies the type of error */
}

The routines report an error whenever they cannot perform the task requested of them. For example, a root-finding function would return a non-zero error code if could not converge to the requested accuracy, or exceeded a limit on the number of iterations. Situations like this are a normal occurrence when using any mathematical library and you should check the return status of the functions that you call.

Whenever a routine reports an error the return value specifies the type of error. The return value is analogous to the value of the variable errno in the C library. However, the C library's errno is a global variable, which is not thread-safe (There can be only one instance of a global variable per program. Different threads of execution may overwrite errno simultaneously). Returning the error number directly avoids this problem. The caller can examine the return code and decide what action to take, including ignoring the error if it is not considered serious.

The error code numbers are defined in the file `gsl_errno.h'. They all have the prefix GSL_ and expand to non-zero constant integer values. Many of the error codes use the same base name as a corresponding error code in C library. Here are some of the most common error codes,

Macro: int GSL_EDOM
Domain error; used by mathematical functions when an argument value does not fall into the domain over which the function is defined (like EDOM in the C library)

Macro: int GSL_ERANGE
Range error; used by mathematical functions when the result value is not representable because of overflow or underflow (like ERANGE in the C library)

Macro: int GSL_ENOMEM
No memory available. The system cannot allocate more virtual memory because its capacity is full (like ENOMEM in the C library). This error is reported when a GSL routine encounters problems when trying to allocate memory with malloc.

Macro: int GSL_EINVAL
Invalid argument. This is used to indicate various kinds of problems with passing the wrong argument to a library function (like EINVAL in the C library).
Here is an example of some code which checks the return value of a function where an error might be reported,

int status = gsl_fft_complex_radix2_forward (data, n);

if (status) {
  if (status == GSL_EINVAL) {
     fprintf (stderr, "invalid argument, n=%d\n", n);
  } else {
     fprintf (stderr, "failed, gsl_errno=%d\n", 
                      status);
  }
  exit (-1);
}

The function gsl_fft_complex_radix2 only accepts integer lengths which are a power of two. If the variable n is not a power of two then the call to the library function will return GSL_EINVAL, indicating that the length argument is invalid. The else clause catches any other possible errors.

The error codes can be converted into an error message using the function gsl_strerror.

Function: const char * gsl_strerror (const int gsl_errno)
This function returns a pointer to a string describing the error code gsl_errno. For example,

printf("error: %s\n", gsl_strerror (status));

would print an error message like error: output range error for a status value of GSL_ERANGE.


Go to the first, previous, next, last section, table of contents.