# 2.5.2.3 Error Handling

LabTalk scripts may be interrupted if an error has been thrown. But there are times when you want to continue the execution of the script even if an error is encountered. In this situation, Origin allows you to use a pair of curly braces ("{" and “}”) to enclose a part of the script that might generate an error. When Origin encounters an error within the section the remaining script up to the "}" is skipped and execution resumes outside the curly braces. In this sense, braces and run.section() commands have the same behavior.

The following is a simple example to show how to handle possible errors. Please note that before executing the scripts in the Script Window, you should create a new worksheet and make sure that column C does not exist.

// Script without error handling
type "Start the section";
stats col(c);
stats.max=;
type "Finished the section";

The line of code, stats col(c);, will throw an error, because Column C does not exist. Then, the script will terminate and only output:

Start the section
Failed to resolve range string, VarName = ix, VarValue = col(c)

Now we will introduce braces to use error handling. We can add a variable to indicate if an error occurred and make use of a System Variable to temporarily shut off Origin error messages:

// Script with error handling
type "Start the section";
int iNOE = @NOE;  // Save current Origin error message output flag
// The section that will generate an error
{
@NOE = 0; // Shut off Origin error messages
vErr = 1; // Set our error variable to true (1)
stats col(c); // This is the code which could produce an error
stats.max=; // Execution will continue only if no error occurs
vErr = 0; // If NO error then our variable gets set to false (0)
}
@NOE = iNOE; // Restore Origin error messages
if(vErr) ty An error occurred. Continuing ...;
type "Finished the section";

The output will become

Start the section
An error occurred. Continuing ...
Finished the section


After the error on the stats col(c) line, code execution continues outside the closing brace (}) and we can trap our error and process as needed. You can comment out the lines related to @NOE if you want the Message Log to retain a record of all errors that occurred.