Trapping errors in blocks of code

The TRY statement provides a convenient syntax for trapping and handling errors that occur within blocks of code. It is normally used in conjunction with the GetLastErrorNumber(), EASIError(), and ReCastEASIError() intrinsics, providing a powerful and flexible technique for generating and trapping errors. The general form of the TRY command is:

TRY
  statement_list_1
ONERROR
  statement_list_2
ENDONERROR

where

statement_list_1 - One or more statements to be executed
                   normally.
statement_list_2 - One or more statements to be executed
                   only if an error occurs while executing
                   statement_list_1.

When execution reaches a TRY statement, the statements in the block immediately after TRY will be executed. If no error occurs during the execution of this block of code, then control will continue with the next statement after the entire TRYONERROR-​ENDONERROR clause. If however an error does occur anywhere within the block, then control will immediately be transferred to the statements after ONERROR. After the error-handling has been executed, control continues with the next statement after the TRY ONERROR-ENDONERROR clause. Note that if an error occurs while executing the ONERROR block, it is treated normally, just as an error occurring immediately outside of the TRY statement.

For example, the Invert() function given above could be coded as follows using the TRY statement:

define function Invert( x )
  local integer y
  try
    y = 1 / x
  onerror
    print "Invert: the following error occurred:"
    print GetLastErrorMessage()
    y = 0
  endonerror
  return( y )
enddefine

© PCI Geomatics Enterprises, Inc.®, 2026. All rights reserved.