Previous: Using GSL error reporting in your own functions, Up: Error Handling   [Index]

3.5 Examples

Here is an example of some code which checks the return value of a function where an error might be reported,

#include <stdio.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_fft_complex.h>

  int status;
  size_t n = 37;


  status = gsl_fft_complex_radix2_forward (data, stride, n);

  if (status) {
    if (status == GSL_EINVAL) {
       fprintf (stderr, "invalid argument, n=%d\n", n);
    } else {
       fprintf (stderr, "failed, gsl_errno=%d\n", 
    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 function call to gsl_set_error_handler_off stops the default error handler from aborting the program. The else clause catches any other possible errors.