7.7.3. EXCEPTION

Used for

Throwing a user-defined exception or re-throwing an exception

Available in

PSQL

Syntax

  1. EXCEPTION [exception_name [custom_message]]
Table 95. EXCEPTION Statement Parameters
ArgumentDescription

exception_name

Exception name

custom_message

Alternative message text to be returned to the caller interface when an exception is thrown. Maximum length of the text message is 1,021 bytes

An EXCEPTION statement throws the user-defined exception with the specified name. An alternative message text of up to 1,021 bytes can optionally override the exception’s default message text.

The exception can be handled in the statement, by just leaving it with no specific WHEN …​ DO handler and allowing the trigger or stored procedure to terminate and roll back all operations. The calling application gets the alternative message text, if any was specified; otherwise, it receives the message originally defined for that exception.

Within the exception-handling block — and only within it — the caught exception can be re-thrown by executing the EXCEPTION statement without parameters. If located outside the block, the re-thrown EXCEPTION call has no effect.

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

Examples

  1. Throwing an exception with dynamically generated text:

    1. EXCEPTION EX_BAD_TYPE
    2. 'Incorrect record type with id ' || new.id;
  2. Throwing an exception upon a condition in the SHIP_ORDER stored procedure:

    1. CREATE OR ALTER PROCEDURE SHIP_ORDER (
    2. PO_NUM CHAR(8))
    3. AS
    4. DECLARE VARIABLE ord_stat CHAR(7);
    5. DECLARE VARIABLE hold_stat CHAR(1);
    6. DECLARE VARIABLE cust_no INTEGER;
    7. DECLARE VARIABLE any_po CHAR(8);
    8. BEGIN
    9. SELECT
    10. s.order_status,
    11. c.on_hold,
    12. c.cust_no
    13. FROM
    14. sales s, customer c
    15. WHERE
    16. po_number = :po_num AND
    17. s.cust_no = c.cust_no
    18. INTO :ord_stat,
    19. :hold_stat,
    20. :cust_no;
    21. IF (ord_stat = 'shipped') THEN
    22. EXCEPTION order_already_shipped;
    23. /* Other statements */
    24. END
  3. Throwing an exception upon a condition and replacing the original message with an alternative message:

    1. CREATE OR ALTER PROCEDURE SHIP_ORDER (
    2. PO_NUM CHAR(8))
    3. AS
    4. DECLARE VARIABLE ord_stat CHAR(7);
    5. DECLARE VARIABLE hold_stat CHAR(1);
    6. DECLARE VARIABLE cust_no INTEGER;
    7. DECLARE VARIABLE any_po CHAR(8);
    8. BEGIN
    9. SELECT
    10. s.order_status,
    11. c.on_hold,
    12. c.cust_no
    13. FROM
    14. sales s, customer c
    15. WHERE
    16. po_number = :po_num AND
    17. s.cust_no = c.cust_no
    18. INTO :ord_stat,
    19. :hold_stat,
    20. :cust_no;
    21. IF (ord_stat = 'shipped') THEN
    22. EXCEPTION order_already_shipped
    23. 'Order status is "' || ord_stat || '"';
    24. /* Other statements */
    25. END
  4. Logging an error and re-throwing it in the WHEN block:

    1. CREATE PROCEDURE ADD_COUNTRY (
    2. ACountryName COUNTRYNAME,
    3. ACurrency VARCHAR(10) )
    4. AS
    5. BEGIN
    6. INSERT INTO country (country,
    7. currency)
    8. VALUES (:ACountryName,
    9. :ACurrency);
    10. WHEN ANY DO
    11. BEGIN
    12. -- write an error in log
    13. IN AUTONOMOUS TRANSACTION DO
    14. INSERT INTO ERROR_LOG (PSQL_MODULE,
    15. GDS_CODE,
    16. SQL_CODE,
    17. SQL_STATE)
    18. VALUES ('ADD_COUNTRY',
    19. GDSCODE,
    20. SQLCODE,
    21. SQLSTATE);
    22. -- Re-throw exception
    23. EXCEPTION;
    24. END
    25. END

See also

CREATE EXCEPTION, WHEN …​ DO