err.texi
来自「开放gsl矩阵运算」· TEXI 代码 · 共 440 行 · 第 1/2 页
TEXI
440 行
@cindex error handlingThis chapter describes the way that GSL functions report and handleerrors. By examining the status information returned by every functionyou can determine whether it succeeded or failed, and if it failed youcan find out what the precise cause of failure was. You can also defineyour own error handling functions to modify the default behavior of thelibrary.The functions described in this section are declared in the header file@file{gsl_errno.h}.@comment Note: In this context we use the word @dfn{error} to mean something@comment different from a bug. An error report from the library just means that@comment the library was not able to compute what you asked. For example, if a@comment root finding function cannot reach the level of precision you requested@comment the library would return an error. In the case of problems caused@comment by real bugs, @pxref{Debugging Numerical Programs}.@menu* Error Reporting:: * Error Handlers:: * Using GSL error reporting in your own functions:: @end menu@node Error Reporting@section Error ReportingThe library follows the thread-safe error reporting conventions of the@sc{posix} Threads library. Functions return a non-zero error code toindicate an error and @code{0} to indicate success.@exampleint status = gsl_function(...)if (status) @{ /* an error occurred */ ..... /* status value specifies the type of error */@}@end exampleThe routines report an error whenever they cannot perform the taskrequested of them. For example, a root-finding function would return anon-zero error code if could not converge to the requested accuracy, orexceeded a limit on the number of iterations. Situations like this area normal occurrence when using any mathematical library and you shouldcheck the return status of the functions that you call.Whenever a routine reports an error the return value specifies thetype of error. The return value is analogous to the value of thevariable @code{errno} in the C library. However, the C library's@code{errno} is a global variable, which is not thread-safe (There canbe only one instance of a global variable per program. Differentthreads of execution may overwrite @code{errno} simultaneously).Returning the error number directly avoids this problem. The caller canexamine the return code and decide what action to take, includingignoring the error if it is not considered serious.The error code numbers are defined in the file @file{gsl_errno.h}. Theyall have the prefix @code{GSL_} and expand to non-zero constant integervalues. Many of the error codes use the same base name as acorresponding error code in C library. Here are some of the most commonerror codes,@cindex error codes@deftypefn {Macro} int GSL_EDOMDomain error; used by mathematical functions when an argument value doesnot fall into the domain over which the function is defined (likeEDOM in the C library)@end deftypefn@deftypefn {Macro} int GSL_ERANGERange error; used by mathematical functions when the result value is notrepresentable because of overflow or underflow (like ERANGE in the Clibrary)@end deftypefn@deftypefn {Macro} int GSL_ENOMEMNo memory available. The system cannot allocate more virtual memorybecause its capacity is full (like ENOMEM in the C library). This erroris reported when a GSL routine encounters problems when trying toallocate memory with @code{malloc}.@end deftypefn@deftypefn {Macro} int GSL_EINVALInvalid argument. This is used to indicate various kinds of problemswith passing the wrong argument to a library function (like EINVAL in the Clibrary). @end deftypefn@noindentHere is an example of some code which checks the return value of afunction where an error might be reported,@exampleint 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);@}@end example@comment @noindentThe function @code{gsl_fft_complex_radix2} only accepts integer lengthswhich are a power of two. If the variable @code{n} is not a powerof two then the call to the library function will return@code{GSL_EINVAL}, indicating that the length argument is invalid. The@code{else} clause catches any other possible errors.The error codes can be converted into an error message using thefunction @code{gsl_strerror}.@deftypefun {const char *} gsl_strerror (const int @var{gsl_errno})This function returns a pointer to a string describing the error code@var{gsl_errno}. For example,@exampleprintf("error: %s\n", gsl_strerror (status));@end example@noindentwould print an error message like @code{error: output range error} for astatus value of @code{GSL_ERANGE}.@end deftypefun@node Error Handlers@section Error Handlers@cindex Error handlersIn addition to reporting errors the library also provides an optionalerror handler. The error handler is called by library functions whenthey report an error, just before they return to the caller. Thepurpose of the handler is to provide a function where a breakpoint canbe set that will catch library errors when running under the debugger.It is not intended for use in production programs, which should handleany errors using the error return codes described above.The default behavior of the error handler is to print a short messageand call @code{abort()} whenever an error is reported by the library.If this default is not turned off then any program using the librarywill 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 checkthe return status of library routines (we don't encourage you to writeprograms this way). If you turn off the default error handler it isyour responsibility to check the return values of the GSL routines. Youcan customize the error behavior by providing a new error handler. Forexample, an alternative error handler could log all errors to a file,ignore certain error conditions (such as underflows), or start thedebugger and attach it to the current process when an error occurs.All GSL error handlers have the type @code{gsl_error_handler_t}, which isdefined in @file{gsl_errno.h},@deftp {Data Type} gsl_error_handler_tThis is the type of GSL error handler functions. An error handler willbe passed four arguments which specify the reason for the error (astring), the name of the source file in which it occurred (also astring), the line number in that file (an integer) and the error number(an integer). The source file and line number are set at compile timeusing the @code{__FILE__} and @code{__LINE__} directives in thepreprocessor. An error handler function returns type @code{void}.Error handler functions should be defined like this,@examplevoid handler (const char * reason, const char * file, int line, int gsl_errno)@end example@end deftp@comment @noindentTo request the use of your own error handler you need to call thefunction @code{gsl_set_error_handler} which is also declared in@file{gsl_errno.h},@deftypefun gsl_error_handler_t gsl_set_error_handler (gsl_error_handler_t @var{new_handler})This functions sets a new error handler, @var{new_handler}, for the GSLlibrary routines. The previous handler is returned (so that you canrestore it later). Note that the pointer to a user defined errorhandler function is stored in a static variable, so there can only beone error handler per program. This function should be not be used inmulti-threaded programs except to set up a program-wide error handlerfrom a master thread. The following example shows how to set andrestore a new error handler,@example/* 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); @end example@noindentTo use the default behavior (@code{abort} on error) set the errorhandler to @code{NULL},@exampleold_handler = gsl_set_error_handler (NULL); @end example@end deftypefun@deftypefun gsl_error_handler_t gsl_set_error_handler_off ()This function turns off the error handler by defining an error handlerwhich does nothing. This will cause the program to continue after anyerror, so the return values from any library routines must be checked.This is the recommended behavior for production programs. The previoushandler is returned (so that you can restore it later).@end deftypefunThe error behavior can be changed for specific applications byrecompiling the library with a customized definition of the
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?