TRY - CATCH block

Use TRY / CATCH blocks to trap runtime exceptions in a delimited code block.

Syntax:

TRY
   instruction
   [...]
CATCH
   instruction
   [...]
END TRY

Usage:

Any language instruction in the TRY block will be executed until an exception is thrown. After an exception the program execution continues in the CATCH block. If no CATCH block is provided, the execution continues after END TRY.

If no exception is raised by the statements between the TRY and CATCH keywords, the instructions in the CATCH section are ignored and the program flow continues after END TRY.

This code example shows a TRY block executing an SQL statement:
TRY
    SELECT COUNT(*) INTO num_cust FROM customers WHERE ord_date <= max_date 
CATCH
    ERROR "Error caught during SQL statement execution:", SQLCA.SQLCODE
END TRY
A TRY block can be compared with WHENEVER ANY ERROR GOTO. Here is the equivalent of the previous code example:
WHENEVER ANY ERROR GOTO catch_error
    SELECT COUNT(*) INTO num_cust FROM customers WHERE ord_date <= max_date 
    GOTO no_error 
LABEL catch_error:
WHENEVER ERROR STOP
    ERROR "Error caught during SQL statement execution:", SQLCA.SQLCODE
LABEL no_error
The TRY statement can be nested in other TRY statements. In this example, the instruction in line #5 will be executed in case of SQL error:
TRY
    TRY
        SELECT COUNT(*) INTO num_cust FROM customers 
    CATCH
        ERROR "Try block 2: ", SQLCA.SQLCODE
    END TRY
CATCH
    ERROR "Try block 1: ", SQLCA.SQLCODE
END TRY
The WHENEVER ERROR RAISE instruction can be used module-wide to define the behavior when an exception occurs in a function that is called from a TRY / CATCH block. If an exception occurs in a statement after the WHENEVER ERROR RAISE instruction, the program flow returns from the function and raises the exception as if it had occurred in the code of the caller. If the exception is thrown in the MAIN block, the program stops because the exception cannot be processed by a caller. In this example, the instruction in line #5 will be executed if an exception occurs in the cust_report() function:
MAIN
    TRY
        CALL cust_report()
    CATCH
        ERROR "An error occurred during report execution: ", STATUS
    END TRY
END MAIN

FUNCTION cust_report()
    WHENEVER ERROR RAISE
    START REPORT cust_rep ...
    ...
END FUNCTION
Important: It is not possible to set a debugger break point at TRY, CATCH or END TRY: The TRY statement is a pseudo statement, the compiler does not generate p-code for this statement.