Back
Home
Up
Next

Exceptions.

    Exceptions are a common mechanism in Object Oriented languages, but they aren't restricted to such languages. The relationship is because in the OO world, the idea of destroying local objects when an exception occurs fits nicely in that paradigm.

An exception may be described as an special object that's created and activated when an unusual condition is met. In OO languages, raising an exception means the stack is unwound to the point where an appropriate exception handler is located, skipping all the code in the middle. This technique is useful to defer to other code section an special condition the current code has no way of dealing with. That code section is usually the calling function.

In IB, you first need to define an exception with a unique name across the database and assign to it a message. All common definition tasks in SQL are carried out through the CREATE reserved word followed by specific words according to the command's syntax. In this case, it's
CREATE EXCEPTION my_exception_name 'message to show'
and such exception's name must be unique across a database. Be careful that the message of the exception is kept currently on a field without character set, so you can see some weird characters depending on the character set (code page) of the client program.

Once you have your exception, it's available to all triggers and stored procedures in the database. Then you can use it by means of the syntax
EXCEPTION my_exception_name
to raise (or throw) it and stop the proper action in a BEFORE trigger. This exception will abort the operation and the changes will be undone. An error message will be returned to the calling program and the procedure or trigger body will be aborted, unless the exception is handled by a WHEN statement. You can think about WHEN as a cousin of C++'s catch and Delphi's except.

In the case of selectable procedures that also perform data changes, an exception only undoes the changes after the last SUSPEND command, but because you'll receive the error in the client application, you can roll back the complete transaction.

 

This page was last updated on 2000-07-02 01:16:10