Handling InterBase Error Codes (Embedded SQL Guide)
From InterBase
Go Up to Additional InterBase Error Handling
Whenever SQLCODE is less than –1, the error status array, isc_status, may contain detailed error information specific to InterBase, including error codes, numbers that uniquely identify each error. With care, error-handling routines can trap for and respond to specific codes.
To trap and handle InterBase error codes in an error-handling routine, follow these steps:
- Check
SQLCODEto be sure it is less than –1. - Check that the first element of the status array is set to
isc_arg_gds, indicating that an InterBase error code is available. In C programs, the first element of the status array isisc_status[0]. - Do
notattempt to handle errors reported in the status array if the first status array element contains a value other than 1. - If
SQLCODEis less than –1 and the first element inisc_statusis set toisc_arg_gds, use the actual InterBase error code in the second element ofisc_statusto branch to an appropriate routine for that error.
Tip:
InterBase error codes are mapped to mnemonic definitions (for example,
InterBase error codes are mapped to mnemonic definitions (for example,
isc_arg_gds) that can be used in code to make it easier to read, understand, and maintain. Definitions for all InterBase error codes can be found in the ibase.h file.The following C code fragment illustrates an error-handling routine that:
- Displays error messages with
isc_print_sqlerror(). - Illustrates how to parse for and handle six specific InterBase errors which might be corrected upon roll back, data entry, and retry.
- Uses mnemonic definitions for InterBase error numbers.
. . .
int c, jval, retry_flag = 0;
jmp_buf jumper;
. . .
main()
{
. . .
jval = setjmp(jumper);
if (retry_flag)
ROLLBACK;
. . .
}
int ErrorHandler(void)
{
retry_flag = 0; /* reset to 0, no retry */
isc_print_sqlerror(SQLCODE, isc_status); /* display errors */
if (SQLCODE < –1)
{
if (isc_status[0] == isc_arg_gds)
{
switch (isc_status[1])
{
case isc_convert_error:
case isc_deadlock:
case isc_integ_fail:
case isc_lock_conflict:
case isc_no_dup:
case isc_not_valid:
printf("\n Do you want to try again? (Y/N)");
c = getchar();
if (c == 'Y' || c == 'y')
{
retry_flag = 1; /* set flag to retry */
longjmp(jumper, 1);
}
break;
case isc_end_arg: /* there really isn’t an error */
retry_flag = 1; /* set flag to retry */
longjump(jumper, 1);
break;
default: /* we can’t handle everything, so abort */
break;
}
}
}
EXEC SQL
ROLLBACK;
EXEC SQL
DISCONNECT ALL;
exit(1);
}