Types of Exceptions - Firebird

Three types of exceptions can occur:

  • SQL errors—that is, SQL messages having a negative SQLCODE.
  • Internal Firebird errors that have to do with concurrency, data, metadata, and environmental conditions. They have a nine-digit error code, usually beginning with 3355, that uniquely identifies the GDSCODE. Most GDSCODEs fall into generic groups beneath SQLCODEs, and you will usually get both a SQLCODE and a GDSCODE when an exception occurs.
  • Custom exceptions that you declare as persistent objects in the database and “invoke” in code when a specified condition is detected.

What Is an Exception?

An exception is simply a message that is generated when an error occurs.

All of the predefined exceptions —SQLCODE and GDSCODE —have text messages associated with them. The default messages are in English, but they don’t have to be. Versions of the messages are available in a few other languages (including pig Latin!), while others are either “works in progress” or “jobs waiting for volunteers.”

Firebird has DDL syntax for creating custom exceptions with text messages up to 78 bytes long. In Firebird 1.5, you can extend your custom exceptions at runtime and replace the text, including context-specific details, to the message that goes back across the wire.

Creating an Exception

Creating an exception is one of the simplest pieces of DDL in the lexicon. The syntax is

CREATE EXCEPTION exception-name <message>;

exception-name is a regular Firebird identifier of 31 characters or less. It must be unique among identifiers for exceptions and, in dialect 3, it can be double quoted and case sensitive.

<message> is a single-quoted string of text in character set NONE. The text is necessarily terse because of the length limit. For example:

CREATE EXCEPTION NO_DOGS 'No dogs allowed!';
COMMIT;

A CREATE EXCEPTION statement needs to be committed, just as any other DDL statement does.

Altering or Dropping an Exception

As SYSDBA or the owner of an exception that is used in stored procedures, you can alter or drop it at any time. If it is used in a trigger, it can only be altered and then only to change the text message. No dependencies are stored for exceptions used by stored procedures. This makes it a problem if you drop one and forget to replace it—it is embarrassing to have an exception occur because of a missing exception!

To drop our NO_DOGS exception:

DROP EXCEPTION NO_DOGS;

To alter it:

ALTER EXCEPTION NO_DOGS 'No dogs allowed except Irish Wolfhounds!';

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Firebird Topics