catch errors

SYNOPSIS

#include "matrix.h"
catch(int err_num, normal_code_to_execute,
        code_to_execute_if_error)
catchall(normal_code_to_execute,
        code_to_exectue_if_error)
tracecatch(normal_code_to_execute, char *fn_name)
catch_FPE()

DESCRIPTION

The catch() macro provides a way of interposing your own error-handling routines and code in the usual error-handling procedures. The catch() macro works like this: The global variable restart (of type jmp_buf) is saved. Then the code normal_code_to_execute is executed. If an error with error number err_num is raised, then code_to_execute_if_error is executed. If an error with another error number is raised, an error will be raised with the same error number as the original error, but will appear to have come from the catch() macro. If no error is raised then the macro will exit and restart is reset to its old values. The catchall() macro works just like the catch() macro except that\hfil\break code_to_execute_if_error is executed if any\/ error is raised. The tracecatch() macro is really a specialised version of the catchall() macro that sets the error-handling flag to print out the underlying error when it is raised. In every case the old error handling status will be restored on exiting the macro. The routine catch_FPE() sets up a signal handler so that if a SIGFPE signal is raised, it is caught and error() is called as appropriate. The error raised by error() is an E_SIGNAL error.

EXAMPLE

main()
{
    MAT  *A;
    PERM *pivot;
    VEC  *x, *b;
      ......
    tracecatch(
        LUfactor(A,pivot);
        LUsolve(A,pivot,b,x);
        , "main");
      ......
would result in the error messages
"lufactor.c", line 28: NULL objects passed in function
            LUfactor()
"junk.c", line 20: NULL objects passed in function main()
Sorry, exiting program
being printed to stdout if one of A or pivot or b were NULL. These messages would also be printed out to stderr if stdout is not a terminal. On the other hand,
catch(E_NULL,
           LUfactor(A,pi);
           LUsolve(A,pi,b,x);
	   , printf("Ooops, found a NULL object\n"));
simply produces the message Ooops, found a NULL object in this case. However, if another error occurs (say, b is the wrong size) then LUsolve() raises an e_SIZES error, and
"junk.c", line 22: sizes of objects don't match in
            function catch()
Sorry, exiting program
is printed out.

SEE ALSO: signal(), error(), set_err_flag(), ERREXIT() etc.

BUGS

If a different error to the one caught in catch() is raised, then the file and line numbers of the original error are lost. In an if--then--else statement, tracecatch() needs to be enclosed by braces ({\tt\{...\}}).

SOURCE FILE: matrix.h