At 04:59 PM 6/4/2010, [email protected] wrote:

I have never used the WHENEVER syntax to trap program-wide error messages.
I've read the info on the help screen but not sure I fully understand how
this could be used. So I'd like to see some real-time uses of this syntax.
I'm also curious if one program calls another whether you have to repeat
the WHENEVER.


Technically, WHENEVER is like a gate; it checks SQLCODE and one continues
through or goes around. This command is similar to the error variable in
that it is placed once in the code and from there is used to check all
subsequent commands. But while the R:BASE error variable is used once
per R:BASE session, WHENEVER is used once per command file or block.
Each command file or block requires a separate WHENEVER command, but
they all share the same R:BASE error variable. There are two parts to
the WHENEVER command: first, determine if an error condition exists;
second, tell the program what to do.

The first part of the WHENEVER clause includes two choices: SQLERROR
which checks SQLCODE for all errors except "data not found" and NOT
FOUND which checks only for a "data not found" error (SQLCODE=100).
Since there can only be one WHENEVER per command file or block, it
checks for data not found errors or for all other errors. This doesn't
mean that you can't check SQLCODE directly in your code for data not
found errors. It means that automatic processing of error handling code
happens only for one or the other.

If the first part of WHENEVER is true, then the second part of the
WHENEVER command is executed. Like the first part, it has two options:
GOTO label or CONTINUE. GOTO label passes program control to the
specified label, usually an error handling routine that displays
appropriate messages and determines the next step in the program.
CONTINUE turns off a previously issued WHENEVER command within the
same command file or block, the GOTO is not processed and the program
continues with the next command line.

One way to use the WHENEVER clause is to place the command, WHENEVER
SQLERROR GOTO errorlab, at the beginning of each command file or block.
If an error occurs at any line in the code, control is transferred to
commands following the label, errorlab. It is not recommended to use
WHENEVER NOT FOUND with DECLARE CURSOR to trap the end of data for the
cursor. Often, cursors are used with UPDATE, INSERT and SELECT commands
that have WHERE clauses. If the WHERE clause returns a "No rows exist
or satisfy the specified clause", the WHENEVER NOT FOUND is triggered
and the GOTO executed even though there may be more rows to be found
with the cursor. Use WHILE SQLCODE <> 100 with DECLARE CURSOR.

SQLERROR does not trap or process NOT FOUND errors, i.e. SQLCODE=100,
it only deals with SQLCODE < 0.

An example:

   WHENEVER SQLERROR GOTO errorlab
   CONNECT RRBYW16
   -- ..... more code here
LABEL errorlab
   SWITCH (SQLCODE)
      CASE '-7'
      CASE '-9'
        CLS
        PAUSE 2 USING 'Error connecting database.' +
        CAPTION '-ERROR-' +
        ICON STOP +
        OPTION MESSAGE_FONT_NAME Tahoma +
        |MESSAGE_FONT_COLOR RED +
        |MESSAGE_FONT_SIZE 11 +
        |THEMENAME Vista CG
        RETURN
        BREAK
      CASE '-2045'
        CLS
        PAUSE 2 USING 'Command did not execute due to syntax error.' +
        CAPTION '-ERROR-' +
        ICON STOP +
        OPTION MESSAGE_FONT_NAME Tahoma +
        |MESSAGE_FONT_COLOR RED +
        |MESSAGE_FONT_SIZE 11 +
        |THEMENAME Vista CG
        GOTO start
        BREAK
      DEFAULT
        CLS
PAUSE 2 USING 'An error has occurred. See your System Administrator.' +
        CAPTION '-ERROR-' +
        ICON STOP +
        OPTION MESSAGE_FONT_NAME Tahoma +
        |MESSAGE_FONT_COLOR RED +
        |MESSAGE_FONT_SIZE 11 +
        |THEMENAME Vista CG
        RETURN
        BREAK
   ENDSW

WHENEVER is only triggered by errors on SQL commands. It is not
affected by non-SQL commands. Use the R:BASE error variable on non-SQL
commands.

This is not a comprehensive listing of what each error handling method
can do, but a beginner's guide to error checking, how it affects your
programs and some of the possible variations. If you are unsure as to
how to use these in your program, consider the areas or commands that
would cause the most pain and suffering. These areas would be considered
excellent candidates for some error checking. If there are areas that
are still unclear, the next best method of learning is trial and error.

Note that these error handling routines process errors returned by
R:BASE. They do not handle other errors such as:

<> answering NO instead of YES
<> wrong data values are returned
<> wrong data values are entered
<> ESC from menu or dialog box without making a selection

These errors need to be tested for using custom program code.

Hope that helps and provides you with some blue's clues ...

Very Best R:egards,

Razzak.


Reply via email to