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


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;
     
       gsl_set_error_handler_off();
     
       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 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.