6. Working with Transactions and Handling Errors : Error Handling : Error Handling in Embedded Applications : Error Trapping Using Whenever Statement
 
Share this page                  
Error Trapping Using Whenever Statement
The WHENEVER statement specifies a particular action to be performed whenever a particular condition is true. Because conditions are set to true by values in the SQLCA sqlcode, the WHENEVER statement responds only to errors generated by embedded SQL database statements. Forms statements do not set sqlcode.
The following conditions indicate errors or warnings:
Warnings/Error
Explanation
sqlwarning
Indicates that the executed SQL database statement produced a warning condition. Sqlwarning becomes true when the SQLCA sqlwarn0 variable is set to W.
sqlerror
Indicates that an error occurred in the execution of the database statement. Sqlerror becomes true when the SQLCA sqlcode variable contains a negative number.
For a complete discussion of all the conditions, see Whenever in the chapter “SQL Statements.”
The actions that can be specified for these conditions are listed in the following table:
Action
Explanation
continue
Execution continues with the next statement.
stop
Prints an error message and terminates the program’s execution. Pending updates are not committed.
goto label
Performs a host language go to.
call procedure
Calls the specified host language procedure. If call sqlprint is specified, a standard sqlprint procedure is called. This procedure prints the error or warning message and continues with the next statement. A database procedure cannot be specified.
In an application program, a WHENEVER statement is in effect until the next WHENEVER statement (or the end of the program). For example, if you put the following statement in your program:
exec sql whenever sqlerror call myhandler;
the DBMS traps errors for all database statements in your program that (physically) follow the WHENEVER statement, to the “myhandler” procedure. A WHENEVER statement does not affect the statements that physically precede it.
The following diagram illustrates the scope of the WHENEVER statement:
If your program includes an SQLCA, error and database procedure messages are not displayed unless your application issues a WHENEVER...SQLPRINT statement, or II_EMBED_SET is set to sqlprint. For details about II_EMBED_SET, see the System Administrator Guide.