Summary:
See also: Flow Control, Fgl Errors.
In case of the abnormal execution of an instruction, the runtime system throws exceptions that can be handled by the program. Actions can be taken based on the class of the exception. There is no way to raise exceptions explicitly; only the runtime system can throw exceptions.
The WHENEVER instruction defines exception handling in a program module, by associating an exception class to an exception action.
WHENEVER [ANY] ERROR { CONTINUE | STOP
| CALL function
| GOTO label }
There are four actions that can be executed if an exception is raised:
There are four types of exceptions, defining the kind of errors that can occur:
Type | Reason | Examples |
ET_STATEMENT |
Error occurred in a statement. | DISPLAY AT invalid coordinates. |
ET_EXPRESSION |
Expression evaluation error. | Division by zero. |
ET_NOTFOUND |
An SQL statement returns status NOTFOUND. | FETCH when cursor is on last row. |
ET_WARNING |
An SQL statement sets sqlca.sqlawarn flag. | Fetched CHAR value has been truncated. |
The exception classes indirectly define the exception type:
Class | Related Exception Type (defines the error reason) | Default Action |
ERROR | ET_STATEMENT |
STOP |
ANY ERROR | ET_STATEMENT and ET_EXPRESSION |
CONTINUE |
NOT FOUND | ET_NOTFOUND |
CONTINUE |
WARNING | ET_WARNING |
CONTINUE |
After executing an SQL statement, you can query STATUS, SQLSTATE, SQLERRMESSAGE and the SQLCA record to get the description of the error. When the statement has been executed with errors, STATUS and SQLCA.SQLCODE contain the SQL Error Code. If no error occurs, STATUS and SQLCA.SQLCODE are set to zero.
You control the result of an SQL statement execution by using the WHENEVER ERROR exception handler:
01
MAIN02
03
DATABASE stores04
05
WHENEVER ERROR CONTINUE06
SELECT COUNT(*) FROM customer07
IF sqlca.sqlcode THEN08
ERROR "SQL Error occurred:", sqlca.sqlcode09
END IF10
WHENEVER ERROR STOP11
12
END MAIN
The SQL Error Codes are not standard. For example, ORACLE returns 903 when a table name does not exist.
By convention, the STATUS and SQLCA.SQLCODE variables always use IBM Informix SQL Error Codes. When using IBM Informix, both STATUS and SQLCA.SQLCODE variables contain the native Informix error code. When using other database servers, the database interface automatically converts native SQL Error Codes to IBM Informix Error Codes. If no equivalent Informix Error Code can be found, the interface returns -6372 in SQLCA.SQLCODE.
If an SQL error occurs when using IBM Informix, the SQLCA variable is filled with standard information as described in the Informix documentation. When using other database servers, the native SQL Error Code is available in the SQLCA.SQLERRD[2] register. SQL Error Codes in SQLCA.SQLERRD[2] are always negative, even if the database server defines positives SQL Error Codes. Additionally, if the target database API supports ANSI SQL states, the SQLSTATE code is returned in SQLCA.SQLERRM.
The NOTFOUND (100) execution status is returned after a FETCH, when no rows are found.
See also: Connections.
01
MAIN02
WHENEVER ERROR CALL error_handler03
DATABASE stores04
SELECT dummy FROM systables WHERE tabid=105
END MAIN06
07
FUNCTION error_handler()08
DISPLAY "Error:", STATUS09
EXIT PROGRAM 110
END FUNCTION
01
MAIN02
DEFINE tabname VARCHAR(50)03
DEFINE rowcount INTEGER04
05
# In the DATABASE statement, no error should occur.06
DATABASE stores07
08
# But in the next procedure, user may enter a wrong table.09
WHENEVER ERROR CONTINUE10
PROMPT "Enter a table name:" FOR tabname11
LET sqlstmt = "SELECT COUNT(*) FROM " || tabname12
PREPARE s FROM sqlstmt13
IF sqlca.sqlcode THEN14
ERROR "SQL Error occurred:", sqlca.sqlcode15
END IF16
EXECUTE s INTO rowcount17
IF sqlca.sqlcode THEN18
ERROR "SQL Error occurred:", sqlca.sqlcode19
END IF20
WHENEVER ERROR STOP21
22
END MAIN