Combining Error-handling Techniques

From InterBase

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 WHENEVER statements.
  • Overrides the WHENEVER SQLERROR statement to force program continuation using the CONTINUE clause.
  • Checks SQLCODE directly.
  • Overrides WHENEVER SQLERROR again 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 */
. . .
}

Advance To: