In addition to reporting errors the library also provides an optional error handler. The error handler is called by library functions when they report an error, just before they return to the caller. The purpose of the handler is to provide a function where a breakpoint can be set that will catch library errors when running under the debugger. It is not intended for use in production programs, which should handle any errors using the error return codes described above.
The default behavior of the error handler is to print a short message
abort() whenever an error is reported by the library.
If this default is not turned off then any program using the library
will stop with a core-dump whenever a library routine reports an error.
This is intended as a fail-safe default for programs which do not check
the return status of library routines (we don't encourage you to write
programs this way). If you turn off the default error handler it is
your responsibility to check the return values of the GSL routines. You
can customize the error behavior by providing a new error handler. For
example, an alternative error handler could log all errors to a file,
ignore certain error conditions (such as underflows), or start the
debugger and attach it to the current process when an error occurs.
All GSL error handlers have the type
gsl_error_handler_t, which is
defined in `gsl_errno.h',
This is the type of GSL error handler functions. An error handler will
be passed four arguments which specify the reason for the error (a
string), the name of the source file in which it occurred (also a
string), the line number in that file (an integer) and the error number
(an integer). The source file and line number are set at compile time
__LINE__ directives in the
preprocessor. An error handler function returns type
Error handler functions should be defined like this,
void handler (const char * reason, const char * file, int line, int gsl_errno)
To request the use of your own error handler you need to call the
gsl_set_error_handler which is also declared in
This functions sets a new error handler, new_handler, for the GSL library routines. The previous handler is returned (so that you can restore it later). Note that the pointer to a user defined error handler function is stored in a static variable, so there can only be one error handler per program. This function should be not be used in multi-threaded programs except to set up a program-wide error handler from a master thread. The following example shows how to set and restore a new error handler,
/* save original handler, install new handler */ old_handler = gsl_set_error_handler (&my_handler); /* code uses new handler */ ..... /* restore original handler */ gsl_set_error_handler (old_handler);
To use the default behavior (
abort on error) set the error
old_handler = gsl_set_error_handler (NULL);
The error behavior can be changed for specific applications by
recompiling the library with a customized definition of the
GSL_ERROR macro in the file `gsl_errno.h'.