The WHENEVER ERROR statement

Since program statements that access the database may be expected to fail occasionally (the row is locked, etc.) the WHENEVER ERROR statement can be used to handle this type of error.

By default, when a runtime error occurs the program will stop. To prevent this happening when SQL statements that access the database fail, surround the SQL statement with WHENEVER ERROR statements, as in this example based on the fetch_cust function in the custquery.4gl program module:

01 IF (p_fetch_flag = 1) THEN
03   FETCH NEXT cust_curs 
04     INTO mr_custrec.*
06   ...

WHENEVER ERROR statements are modular in scope, and generate additional code for exception handling when the module is compiled. This exception handling is valid until the end of the module or until a new WHENEVER ERROR instruction is encountered by the compiler.

When the example code is compiled, WHENEVER ERROR CONTINUE will generate code to prevent the program from stopping if the FETCH statement fails. Immediately after the FETCH statement, the WHENEVER ERROR STOP instruction will generate the code to reset the default behavior for the rest of the module.

You can write your own error function to handle SQL errors, and use the WHENEVER ERROR CALL <function-name> syntax to activate it. Runtime errors may be logged to an error log.