informix
Informix Guide to SQL: Syntax
SPL Statements

ON EXCEPTION

Use the ON EXCEPTION statement to specify the actions that are taken for a particular error or a set of errors.

Syntax

Element Purpose Restrictions Syntax
error_data_var SPL variable that contains a string returned by an SQL error or a user-defined exception Must be a character data type to receive the error information. Must be valid in the current statement block. Identifier, p. 4-205
error_number SQL error number, or an error number created by a RAISE EXCEPTION statement, that is to be trapped Must be of integer data type. Must be valid in the current statement block. Literal number, p. 4-237
ISAM_error_var Variable that receives the ISAM error number of the exception raised Must be of integer data type. Must be valid in the current statement block. Identifier, p. 4-205
SQL_error_var Variable that receives the SQL error number of the exception raised Must be a character data type. Must be valid in the current statement block. Identifier, p. 4-205

Usage

The ON EXCEPTION statement, together with the RAISE EXCEPTION statement, provides an error-trapping and error-recovery mechanism for SPL. The ON EXCEPTION statement defines a list of errors that you want to trap as the SPL routine executes and specifies the action (within the statement block) to take when the trap is triggered. If the IN clause is omitted, all errors are trapped.

You can use more than one ON EXCEPTION statement within a given statement block.

The scope of an ON EXCEPTION statement is the statement block that follows the ON EXCEPTION statement and all the statement blocks that are nested within that following statement block.

The exceptions that are trapped can be either system- or user-defined.

When an exception is trapped, the error status is cleared.

If you specify a variable to receive an ISAM error, but no accompanying ISAM error exists, a zero (0) returns to the variable. If you specify a variable to receive the returned error text, but none exists, an empty string goes into the variable.

An ON EXCEPTION statement does not work in a udr that is called by a trigger.

Placement of the ON EXCEPTION Statement

ON EXCEPTION is a declarative statement, not an executable statement. For this reason, you must use the ON EXCEPTION statement before any executable statement and after any DEFINE statement in an SPL routine.

The following example shows the correct placement of an ON EXCEPTION statement. The add_salesperson function inserts a set of values into a table. If the table does not exist, it is created, and the values are inserted. The function also returns the total number of rows in the table after the insert occurs.

When an error occurs, the database server searches for the last declaration of the ON EXCEPTION statement, which traps the particular error code. The ON EXCEPTION statement can have the error number in the IN clause or have no IN clause. If the database server finds no pertinent ON EXCEPTION statement, the error code passes back to the caller (the SPL routine, application, or interactive user), and execution aborts.

The following example uses two ON EXCEPTION statements with the same error number so that error code 691 can be trapped in two levels of nesting:

Using the IN Clause to Trap Specific Exceptions

A trap is triggered if either the SQL error code or the ISAM error code matches an exception code in the list of error numbers. The search through the list begins from the left and stops with the first match.

You can use a combination of an ON EXCEPTION statement without an IN clause and one or more ON EXCEPTION statements with an IN clause to set up default trapping. When an error occurs, the database server searches for the last declaration of the ON EXCEPTION statement that traps the particular error code.

A summary of the sequence of statements in the previous example would be:

  1. Test for an error.
  2. If error -210, -211, or -212 occurs, take action A.
  3. If error -300 occurs, take action B.
  4. If any other error occurs, take action C.

Receiving Error Information in the SET Clause

If you use the SET clause, when an exception occurs, the SQL error code and (optionally) the ISAM error code are inserted into the variables that are specified in the SET clause. If you provided an error_data_var, any error text that the database server returns is put into the error_data_var. Error text includes information such as the offending table or column name.

Forcing Continuation of the Routine

The first example in Placement of the ON EXCEPTION Statement uses the WITH RESUME keyword to indicate that after the statement block in the ON EXCEPTION statement executes, execution is to continue at the LET x = SELECT COUNT(*) FROM emp_list statement, which is the line following the line that raised the error. For this function, the result is that the count of salespeople names occurs even if the error occurred.

Continuing Execution After an Exception Occurs

If you do not include the WITH RESUME keyword in your ON EXCEPTION statement, the next statement that executes after an exception occurs depends on the placement of the ON EXCEPTION statement, as the following scenarios describe:

Errors Within the ON EXCEPTION Statement Block

To prevent an infinite loop, if an error occurs during execution of the statement block of an error trap, the search for another trap does not include the current trap.


Informix Guide to SQL: Syntax, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved