TRY

The TRY command provides a convenient syntax for trapping and handling errors within blocks of code. It is typically used in conjunction with the F$ERRNUM, EASIError, and ReCastEASIError intrinsics.

 TRY
    statement_list_1
 ONERROR
    statement_list_2
 ENDONERROR

 statement_list_1 - One or more statements.
 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-ONERROR-​ENDONERROR clause, the statements after TRY will be executed (statement_list_1). If no error occurs during the execution of this block of code, then control continues with the next statement after the entire TRY-ONERROR-ENDONERROR clause. If, however, an error does occur, then control will immediately be transferred to the statements after ONERROR (statement_list_2). After the error-handling has been executed, control continues with the next statement after the TRY-ONERROR-ENDONERROR clause.

Example:

Define a simple procedure to compute frequency from period. It returns a large value if period is zero and raises an error if some other error is encountered, such as a non-numeric value for period.

 DEFINE FUNCTION freq( period )
    local double frequency
    local int error

    TRY
       frequency = 1 / period
    ONERROR
       ! extract the error
       error = F$ERRNUM()
   
       ! if the error was divide-by-zero, then set frequency to a large value
       IF ( error = 608 ) THEN
          frequency = 1.0e+10
       ! otherwise, something unexpected happened, re-cast the error
       ELSE
          CALL ReCastEASIError()
       ENDIF
    ENDONERROR

    return( frequency )
 ENDDEFINE

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