Combining Error-handling Techniques
Go Up to Standard Error Handling
Error handling in many programs can benefit from combining WHENEVER with direct checking of SQLCODE. A program might include generic WHENEVER statements for handling most SQL error conditions, but for critical operations, WHENEVER statements might be temporarily overridden to enable direct checking of SQLCODE.
For example, the following C code:
- Sets up generic error handling with three
WHENEVERstatements. - Overrides the
WHENEVER SQLERRORstatement to force program continuation using theCONTINUEclause. - Checks
SQLCODEdirectly. - Overrides
WHENEVER SQLERRORagain to reset it.
main()
{
EXEC SQL
WHENEVER SQLERROR GOTO ErrorExit; /* trap all errors */
EXEC SQL
WHENEVER SQLWARNING GOTO WarningExit; /* trap warnings */
EXEC SQL
WHENEVER NOT FOUND GOTO AllDone; /* trap end of file */
. . .
EXEC SQL
WHENEVER SQLERROR CONTINUE; /* prevent trapping of errors */
EXEC SQL
SELECT CITY INTO :city FROM STATES
WHERE STATE = :stat:statind;
switch (SQLCODE)
{
case 0:
break; /* NO ERROR */
case –1
ErrorTooMany();
break;
case 100:
ErrorNotFound();
break;
default:
ErrorExitFunction(); /* Handle all other errors */
break;
}
EXEC SQL
WHENEVER SQLERROR GOTO ErrorExit; /* reset to trap all errors */
. . .
}