~ubuntu-branches/ubuntu/dapper/gsl-ref-html/dapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.54+ (gsl)
     from /home/bjg/gsl.redhat/doc/gsl-ref.texi on 14 September 2005 -->

<TITLE>GNU Scientific Library -- Reference Manual - Error Handling</TITLE>
<link href="gsl-ref_4.html" rel=Next>
<link href="gsl-ref_2.html" rel=Previous>
<link href="gsl-ref_toc.html" rel=ToC>

</HEAD>
<BODY>
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_2.html">previous</A>, <A HREF="gsl-ref_4.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC26" HREF="gsl-ref_toc.html#TOC26">Error Handling</A></H1>
<P>
<A NAME="IDX45"></A>
<A NAME="IDX46"></A>
This chapter describes the way that GSL functions report and handle
errors.  By examining the status information returned by every function
you can determine whether it succeeded or failed, and if it failed you
can find out what the precise cause of failure was.  You can also define
your own error handling functions to modify the default behavior of the
library.

</P>
<P>
The functions described in this section are declared in the header file
<TT>`gsl_errno.h'</TT>.

</P>



<H2><A NAME="SEC27" HREF="gsl-ref_toc.html#TOC27">Error Reporting</A></H2>

<P>
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 <CODE>0</CODE> to indicate success.

</P>

<PRE>
int status = gsl_function (...)

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

<P>
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.

</P>
<P>
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
<CODE>errno</CODE> in the C library.  The caller can examine the return code
and decide what action to take, including ignoring the error if it is
not considered serious.

</P>
<P>
In addition to reporting errors by return codes the library also has an
error handler function <CODE>gsl_error</CODE>.  This function is called by
other library functions when they report an error, just before they
return to the caller.  The default behavior of the error handler is to
print a message and abort the program,

</P>

<PRE>
gsl: file.c:67: ERROR: invalid argument supplied by user
Default GSL error handler invoked.
Aborted
</PRE>

<P>
The purpose of the <CODE>gsl_error</CODE> 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 return codes.

</P>


<H2><A NAME="SEC28" HREF="gsl-ref_toc.html#TOC28">Error Codes</A></H2>

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

</P>
<P>
<A NAME="IDX47"></A>
<DL>
<DT><U>Macro:</U> int <B>GSL_EDOM</B>
<DD><A NAME="IDX48"></A>
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)
</DL>

</P>
<P>
<DL>
<DT><U>Macro:</U> int <B>GSL_ERANGE</B>
<DD><A NAME="IDX49"></A>
Range error; used by mathematical functions when the result value is not
representable because of overflow or underflow (like ERANGE in the C
library)
</DL>

</P>
<P>
<DL>
<DT><U>Macro:</U> int <B>GSL_ENOMEM</B>
<DD><A NAME="IDX50"></A>
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 <CODE>malloc</CODE>.
</DL>

</P>
<P>
<DL>
<DT><U>Macro:</U> int <B>GSL_EINVAL</B>
<DD><A NAME="IDX51"></A>
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). 
</DL>

</P>
<P>
The error codes can be converted into an error message using the
function <CODE>gsl_strerror</CODE>.

</P>
<P>
<DL>
<DT><U>Function:</U> const char * <B>gsl_strerror</B> <I>(const int <VAR>gsl_errno</VAR>)</I>
<DD><A NAME="IDX52"></A>
This function returns a pointer to a string describing the error code
<VAR>gsl_errno</VAR>. For example,

</P>

<PRE>
printf ("error: %s\n", gsl_strerror (status));
</PRE>

<P>
would print an error message like <CODE>error: output range error</CODE> for a
status value of <CODE>GSL_ERANGE</CODE>.
</DL>

</P>


<H2><A NAME="SEC29" HREF="gsl-ref_toc.html#TOC29">Error Handlers</A></H2>
<P>
<A NAME="IDX53"></A>

</P>
<P>
The default behavior of the GSL error handler is to print a short
message and call <CODE>abort()</CODE>.  When this default is in use programs
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).

</P>
<P>
If you turn off the default error handler it is your responsibility to
check the return values of routines and handle them yourself.  You can
also 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.

</P>
<P>
All GSL error handlers have the type <CODE>gsl_error_handler_t</CODE>, which is
defined in <TT>`gsl_errno.h'</TT>,

</P>
<P>
<DL>
<DT><U>Data Type:</U> <B>gsl_error_handler_t</B>
<DD><A NAME="IDX54"></A>

</P>
<P>
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
using the <CODE>__FILE__</CODE> and <CODE>__LINE__</CODE> directives in the
preprocessor.  An error handler function returns type <CODE>void</CODE>.
Error handler functions should be defined like this,

</P>

<PRE>
void handler (const char * reason, 
              const char * file, 
              int line, 
              int gsl_errno)
</PRE>

</DL>
<P>
To request the use of your own error handler you need to call the
function <CODE>gsl_set_error_handler</CODE> which is also declared in
<TT>`gsl_errno.h'</TT>,

</P>
<P>
<DL>
<DT><U>Function:</U> gsl_error_handler_t * <B>gsl_set_error_handler</B> <I>(gsl_error_handler_t <VAR>new_handler</VAR>)</I>
<DD><A NAME="IDX55"></A>

</P>
<P>
This function sets a new error handler, <VAR>new_handler</VAR>, 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 be only
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,

</P>

<PRE>
/* save original handler, install new handler */
old_handler = gsl_set_error_handler (&#38;my_handler); 

/* code uses new handler */
.....     

/* restore original handler */
gsl_set_error_handler (old_handler); 
</PRE>

<P>
To use the default behavior (<CODE>abort</CODE> on error) set the error
handler to <CODE>NULL</CODE>,

</P>

<PRE>
old_handler = gsl_set_error_handler (NULL); 
</PRE>

</DL>

<P>
<DL>
<DT><U>Function:</U> gsl_error_handler_t * <B>gsl_set_error_handler_off</B> <I>()</I>
<DD><A NAME="IDX56"></A>
This function turns off the error handler by defining an error handler
which does nothing. This will cause the program to continue after any
error, so the return values from any library routines must be checked.
This is the recommended behavior for production programs.  The previous
handler is returned (so that you can restore it later).
</DL>

</P>
<P>
The error behavior can be changed for specific applications by
recompiling the library with a customized definition of the
<CODE>GSL_ERROR</CODE> macro in the file <TT>`gsl_errno.h'</TT>.

</P>


<H2><A NAME="SEC30" HREF="gsl-ref_toc.html#TOC30">Using GSL error reporting in your own functions</A></H2>
<P>
<A NAME="IDX57"></A>
If you are writing numerical functions in a program which also uses GSL
code you may find it convenient to adopt the same error reporting
conventions as in the library.

</P>
<P>
To report an error you need to call the function <CODE>gsl_error</CODE> with a
string describing the error and then return an appropriate error code
from <CODE>gsl_errno.h</CODE>, or a special value, such as <CODE>NaN</CODE>.  For
convenience the file <TT>`gsl_errno.h'</TT> defines two macros which carry
out these steps:

</P>
<P>
<DL>
<DT><U>Macro:</U> <B>GSL_ERROR</B> <I>(<VAR>reason</VAR>, <VAR>gsl_errno</VAR>)</I>
<DD><A NAME="IDX58"></A>

</P>
<P>
This macro reports an error using the GSL conventions and returns a
status value of <CODE>gsl_errno</CODE>.  It expands to the following code fragment,

</P>

<PRE>
gsl_error (reason, __FILE__, __LINE__, gsl_errno);
return gsl_errno;
</PRE>

<P>
The macro definition in <TT>`gsl_errno.h'</TT> actually wraps the code
in a <CODE>do { ... } while (0)</CODE> block to prevent possible
parsing problems.
</DL>

</P>
<P>
Here is an example of how the macro could be used to report that a
routine did not achieve a requested tolerance.  To report the error the
routine needs to return the error code <CODE>GSL_ETOL</CODE>.

</P>

<PRE>
if (residual &#62; tolerance) 
  {
    GSL_ERROR("residual exceeds tolerance", GSL_ETOL);
  }
</PRE>

<P>
<DL>
<DT><U>Macro:</U> <B>GSL_ERROR_VAL</B> <I>(<VAR>reason</VAR>, <VAR>gsl_errno</VAR>, <VAR>value</VAR>)</I>
<DD><A NAME="IDX59"></A>

</P>
<P>
This macro is the same as <CODE>GSL_ERROR</CODE> but returns a user-defined
value of <VAR>value</VAR> instead of an error code.  It can be used for
mathematical functions that return a floating point value.
</DL>

</P>
<P>
The following example shows how to return a <CODE>NaN</CODE> at a mathematical
singularity using the <CODE>GSL_ERROR_VAL</CODE> macro,

</P>

<PRE>
if (x == 0) 
  {
    GSL_ERROR_VAL("argument lies on singularity", 
                  GSL_ERANGE, GSL_NAN);
  }
</PRE>



<H2><A NAME="SEC31" HREF="gsl-ref_toc.html#TOC31">Examples</A></H2>

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

</P>

<PRE>
#include &#60;stdio.h&#62;
#include &#60;gsl/gsl_errno.h&#62;
#include &#60;gsl/gsl_fft_complex.h&#62;

...
  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);
  }
...
</PRE>

<P>
The function <CODE>gsl_fft_complex_radix2</CODE> only accepts integer lengths
which are a power of two.  If the variable <CODE>n</CODE> is not a power of
two then the call to the library function will return <CODE>GSL_EINVAL</CODE>,
indicating that the length argument is invalid.  The function call to
<CODE>gsl_set_error_handler_off()</CODE> stops the default error handler from
aborting the program.  The <CODE>else</CODE> clause catches any other possible
errors.

</P>

<P><HR><P>
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_2.html">previous</A>, <A HREF="gsl-ref_4.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
</BODY>
</HTML>