How to allow error recovery

I'm running multiple files in Gauss containing econometric tests for various datasets. My gauss file calls multiple other gauss files and looks like this:

run -r testcarrot.gss;
run -r testapple.gss;
run -r testpear.gss;

Some of these tests fail and some succeed. I would like the script to continue running after an error as occurred. Does Gauss have a try() statement similar to the R language? Let's say for example that testapple.gss fails, I would like the script to continue and run testpear.gss.

The error handling section of the GAUSS Language reference (page 95) refers to trap. A description page 1710 explains that it "Sets the trap flag to enable or disable trapping of numerical errors." But the example is obscure to me I couldn't really figure out how to use trap and trapchk before and after the run statements.

Some guidance would be welcomed at this stage.

1 Answer


The trap command has to be set inside of the program that you are executing with 'run -r'. With that said, let's work through some very simple trap examples. Let's start with some code that will fail with a numerical error:

//Create a singular matrix
x = ones(4,4);

//Attempt to calculate inverse
x_inv = inv(x);

The code above will fail with error G0048: Matrix singular, because the matrix IS singular. However, the program is not necessarily incorrect and we may want our program to handle the error and continue. To suppress the error return, turn on trap like this:

//Enable error trapping
trap 1;

//Create a singular matrix
x = ones(4,4);

//Attempt to calculate inverse
x_inv = inv(x);

Now this code will not return an error. But this brings up two questions: How will my program know that the inverse failed? What is the value of x_inv after the program run?

If trap is set in the manner above, then the function that failed will return a scalar missing value. You can check to see if the return is a scalar missing value with the scalmiss function like this:

//Enable error trapping
trap 1;

//Create a singular matrix
x = ones(4,4);

//Attempt to calculate inverse
x_inv = inv(x);

//Check to see if 'inv' failed
if scalmiss(x_inv);
    //code for error case here
    print "'x' was singular this time";

    //end this program, but do not kill
    //any others that were started with 'run -r'

Borrowing your fruit motif, if we save the code from the section immediately above to a file named trap_apple.gss and then create a file named trap_drive.gss with the following code:

run -r trap_apple.gss;

//Print message to indicate whether our
//program came back from trap_apple.gss
print "back to main";

I think you will see how to do what you are trying to do. Please feel free to post if any other questions come up.



Your Answer

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 30 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy