(Contents)(Previous)(Next)

The Whenever Statement

The function of whenever statements is to perform general error and exception handling routines for all subsequent SQL statements. Different kinds of errors and exceptions can thereby be distinguished. In application programming, whenever statements help to handle error situations. Whenever statements should always be used when 'sqlcode' or 'sqlwarning' is not checked after every individual SQL statement. Those SQL statements are considered to be subsequent that textually (statically) follow the whenever statement in the program. A whenever error handling routine is valid until it is changed by another whenever statement.

The whenever statement checks four classes of ADABAS return codes (values of sqlwarn0 or sqlcode) and offers four standard actions that can be taken in response.

The general format of the whenever statement is:

whenever <condition> <action>

One of the following cases can be specified for <condition>:

sqlwarning exists when 'sqlwarn0' is set to 'W'. Then, at least one 'sqlwarning' is set.

sqlerror indicates that 'sqlcode' has a negative value, which means that an unexpected error occurred. The program should be aborted and analyzed. The possible error codes and adequate user actions in response to them are described in the manual Messages and Codes.

sqlexception indicates a positive sqlcode value greater than 100.

Messages of this kind generally are exceptional cases which should be handled within the program.

not found is valid when no (further) table row has been found and 'sqlcode' has the value +100 (ROW NOT FOUND).

One of the following cases can be specified for <action>:

stop causes the regular resetting of the current transaction and abortion of the program (COMMIT WORK RELEASE).

continue does not perform an action when the condition occurs and therefore suspends another whenever condition which has previously been set.

go to <lab> causes a jump to the indicated label (maximum length of the label: 45 characters).

call <proc> has the effect that the specified function will be executed. The function call may have a maximum length of 50 characters, parameters inclusive.

If no whenever statements are included, 'continue' is the default action for all conditions. User error handling can be implemented by checking 'sqlcode'. If another whenever statement has previously been specified, it must be suspended by 'whenever ... continue'.

If SQL statements are issued in the whenever error handling code, then the corresponding whenever action must be set to 'continue' in order to avoid an endless loop generated by repeated calls of the whenever error handling routine.

The whenever statements can also be used to call C functions before and after each SQL statement (see chapter Whenever Statements).

Example:

In this example, the procedure 'warnproc' is called for all SQL statements when 'sqlwarn0' is set. The standard error handling for a negative sqlcode, on the other hand, varies. Only the select statement lies within the scope of the first whenever sqlerror statement. The second whenever sqlerror statement refers to all subsequent SQL statements. The delete statement is not placed within the scope of a (preceding) whenever sqlerror statement. Therefore, the default action 'continue' is operative.


(Contents)(Previous)(Next)