5.16. EXCEPTION

This section describes how to create, modify and delete custom exceptions for use in PSQL modules.

5.16.1. CREATE EXCEPTION

Creates a custom exception for use in PSQL modules

Available inDSQL, ESQL

Syntax

CREATE EXCEPTION [IF NOT EXISTS]
  [exc-schema <period>] exception_name
  '<message>'
 
<message> ::= <message-part> [<message-part> ...]
 
<message-part> ::=
    <text>
  | @<slot>
 
<slot> ::= one of 1..9

Table 5.45. CREATE EXCEPTION Statement Parameters
ParameterDescription

exc-schema

Schema of the exception. If not specified, the exception will be created in the current schema; for ALTER, etc., the exception will be located on the search path.

exception_name

Exception name. The maximum length is 63 characters.

message

Default error message. The maximum length is 1,021 characters.

text

Text of any character

slot

Slot number of a parameter. Numbering starts at 1. Maximum slot number is 9.

The statement CREATE EXCEPTION creates a new exception for use in PSQL modules. If an exception with the same name and schema exists, the statement will raise an error.

The exception name is an identifier, see Identifiers for more information.

When IF NOT EXISTS is specified, the statement will not raise an error if the exception already exists.

The default message is stored in character set NONE, i.e. in characters of any single-byte character set. The text can be overridden in the PSQL code when the exception is thrown.

The error message may contain parameter slots that can be filled when raising the exception.

🛑
Warning

A parameter slot number is a single digit! A second and subsequent digits are treated as literal text. For example, @10 will be interpreted as slot 1 followed by a literal 0.

Custom exceptions are stored in the system table RDB$EXCEPTIONS.

5.16.1.1. Who Can Create an Exception

The CREATE EXCEPTION statement can be executed by:

The user executing the CREATE EXCEPTION statement becomes the owner of the exception.

5.16.1.2. CREATE EXCEPTION Examples

Creating an exception named E_LARGE_VALUE

CREATE EXCEPTION E_LARGE_VALUE
  'The value is out of range';

Creating a parameterized exception E_INVALID_VALUE

CREATE EXCEPTION E_INVALID_VALUE
  'Invalid value @1 for field @2';

See alsoSection 5.16.2, “ALTER EXCEPTION”, Section 5.16.3, “CREATE OR ALTER EXCEPTION”, Section 5.16.4, “DROP EXCEPTION”, Section 5.16.5, “RECREATE EXCEPTION”

5.16.2. ALTER EXCEPTION

Alters the default message of a custom exception

Available inDSQL, ESQL

Syntax

ALTER EXCEPTION [exc-schema <period>] exception_name
  '<message>'
 
!! See syntax of CREATE EXCEPTION for further rules !!

5.16.2.1. Who Can Alter an Exception

The ALTER EXCEPTION statement can be executed by:

  • Administrators

  • The owner of the exception

  • Users with the ALTER ANY EXCEPTION privilege

5.16.2.2. ALTER EXCEPTION Examples

Changing the default message for the exception E_LARGE_VALUE

ALTER EXCEPTION E_LARGE_VALUE
  'The value exceeds the prescribed limit of 32,765 bytes';

See alsoSection 5.16.1, “CREATE EXCEPTION”, Section 5.16.3, “CREATE OR ALTER EXCEPTION”, Section 5.16.4, “DROP EXCEPTION”, Section 5.16.5, “RECREATE EXCEPTION”

5.16.3. CREATE OR ALTER EXCEPTION

Creates a custom exception if it doesn’t exist, or alters a custom exception

Available inDSQL

Syntax

CREATE OR ALTER EXCEPTION [exc-schema <period>] exception_name
  '<message>'
 
!! See syntax of CREATE EXCEPTION for further rules !!

The statement CREATE OR ALTER EXCEPTION is used to create the specified exception if it does not exist, or to modify the text of the error message returned from it if it exists already. If an existing exception is altered by this statement, any existing dependencies will remain intact.

5.16.3.1. CREATE OR ALTER EXCEPTION Example

Changing the message for the exception E_LARGE_VALUE

CREATE OR ALTER EXCEPTION E_LARGE_VALUE
  'The value is higher than the permitted range 0 to 32,765';

See alsoSection 5.16.1, “CREATE EXCEPTION”, Section 5.16.2, “ALTER EXCEPTION”, Section 5.16.5, “RECREATE EXCEPTION”

5.16.4. DROP EXCEPTION

Drops a custom exception

Available inDSQL, ESQL

Syntax

DROP EXCEPTION [IF EXISTS]
  [exc-schema <period>] exception_name

The statement DROP EXCEPTION is used to delete an exception. Any dependencies on the exception will cause the statement to fail, and the exception will not be deleted.

When IF EXISTS is specified, the statement will not raise an error if the exception does not exist.

5.16.4.1. Who Can Drop an Exception

The DROP EXCEPTION statement can be executed by:

  • Administrators

  • The owner of the exception

  • Users with the DROP ANY EXCEPTION privilege

5.16.4.2. DROP EXCEPTION Examples

Dropping exception E_LARGE_VALUE

DROP EXCEPTION E_LARGE_VALUE;

See alsoSection 5.16.1, “CREATE EXCEPTION”, Section 5.16.5, “RECREATE EXCEPTION”

5.16.5. RECREATE EXCEPTION

Drops a custom exception if it exists, and creates a custom exception

Available inDSQL

Syntax

RECREATE EXCEPTION [exc-schema <period>] exception_name
  '<message>'
 
!! See syntax of CREATE EXCEPTION for further rules !!

The statement RECREATE EXCEPTION creates a new exception for use in PSQL modules. If an exception with the same name exists already, the RECREATE EXCEPTION statement will try to drop it and create a new one. If there are any dependencies on the existing exception, the attempted deletion fails and RECREATE EXCEPTION is not executed.

5.16.5.1. RECREATE EXCEPTION Example

Recreating the E_LARGE_VALUE exception

RECREATE EXCEPTION E_LARGE_VALUE
  'The value exceeds its limit';

See alsoSection 5.16.1, “CREATE EXCEPTION”, Section 5.16.4, “DROP EXCEPTION”, Section 5.16.3, “CREATE OR ALTER EXCEPTION”