2.4.1.4 X-Function Exception Handling
The example below illustrates trapping an X-Function error with LabTalk, so that an X-Function call that is likely to generate an error does not break your entire script.
For X-Functions that do not return an error code, two functions exist to check for errors in the last executed X-Function: xf_get_last_error_code() and xf_get_last_error_message()$. These functions should be used in situations where the potential exists that a particular X-Function could fail.
In this example, the user is given the option of selecting a file for import, but if that import fails (e.g. user picked file type inappropriate for the import) we need to handle the remaining code.
dlgfile gr:=*.txt; // Get the file name and path from user impasc -se; // Need to use -se switch for execution to continue, see note below if( 0 != xf_get_last_error_code() ) { strError$ = "XFunction Failed: " + xf_get_last_error_message()$; type strError$; break 1; // Stop execution } // Data import probably succeeded, so our script can continue type continuing...;
Note the use of the general X-Function option -se to suppress error messages. You can also use -sl to suppress error logging and -sb to suppress both. It is necessary to use one of these options in order for script execution to continue to the next line when the X-Function call fails.
Looping Over to Find Peaks
In the following example, we loop over all columns in a worksheet to find peaks. If no peak is found in a particular column, the script continues with the rest of the columns. It is assumed here that a worksheet with suitable data is active.
//import data newbook; string fn$=system.path.program$ + "Samples\curve fitting\Asymmetric Gaussian.dat"; impasc fname:=fn$ ; //loop over all columns for(int ii=1; ii<=wks.ncols; ii++) { // Find peak in current column, suppress error message from XF Dataset mypeaks; pkfind $(ii) ocenter:=mypeaks -se; // Need to use -se for execution to continue // Check to see if XF failed if( 0 != xf_get_last_error_code() ) { type "Failed on column $(ii): %(xf_get_last_error_message()$)"; } else { type Found $(mypeaks.getsize()) peaks in column $(ii); } }