WHENEVER instruction

The WHENEVER instruction defines exception handling in a program module, by associating an exception class with an exception action.

Syntax

WHENEVER exception-class
 exception-action

where exception-class is one of:

{ [ANY] ERROR
| [ANY]  SQLERROR
|  NOT FOUND
|  WARNING
}

and exception-action is one of:

{ CONTINUE
| STOP
| CALL function 
| RAISE
| GOTO label
}
  1. function can be any function name defined in the program.
  2. label must be a label defined in the current program block (main, function or report routine).

Usage

The scope of a WHENEVER instruction is similar to a C preprocessor macro. It is local to the module and valid until the end of the module, unless a new WHENEVER instruction is encountered by the compiler.

This code example shows a typical WHENEVER instruction usage:

WHENEVER ERROR CONTINUE
DROP TABLE mytable -- SQL error will be ignored 
CREATE TABLE mytable ( k INT, c VARCHAR(20) )
WHENEVER ERROR STOP
IF SQLCA.SQLCODE != 0 THEN
   ERROR "Could not create the table..."
END IF

Exception classes ERROR and SQLERROR are synonyms (compatibility issue). The previous example could have used WHENEVER SQLERROR instead of WHENEVER ERROR.

Actions for classes ERROR, WARNING and NOT FOUND can be set independently:

WHENEVER ERROR STOP
WHENEVER WARNING CONTINUE
WHENEVER NOT FOUND GOTO not_found_handler 
...

For SQL instructions that can potentially generate errors, it is recommended that you define an exception handler locally; errors in the rest of the program can be handled by the default exception handler.

The RAISE option can be used to propagate the error to the caller, which typically traps the error in a TRY/CATCH block.

WHENEVER [ANY] ERROR RAISE is not supported in a REPORT routine.