Sub topic 3
Public Constants, Types and Exceptions in the Specification
This chapter assumes you know basic SQL, PL/SQL variables and control flow, standalone subprograms, and basic exception handling.
A package specification can expose more than procedures and functions. It can publish constants, types, and named exceptions. These declarations become part of the package's public interface. Code outside the package can refer to them through the package name.
Public constants
A constant is a value that cannot be changed after it is initialized. A package constant is useful when callers and the package body must use the same rule.
CREATE OR REPLACE PACKAGE order_rules AS
c_max_items CONSTANT PLS_INTEGER := 100;
c_currency CONSTANT VARCHAR2(3) := 'USD';
END order_rules;
/A caller uses dot notation to access a public constant:
BEGIN
DBMS_OUTPUT.PUT_LINE(
'Maximum items: ' || order_rules.c_max_items
);
END;
/The constant declaration belongs in the specification when callers need to use it. A constant declared only in the body is private.
Public PL/SQL types
A package specification can define types for data exchanged with its procedures and functions. Common choices include subtypes, records, and collections.
A subtype gives an existing datatype a more specific meaning or constraint. A record groups related fields. An associative array is an in-memory collection indexed by a key.
CREATE OR REPLACE PACKAGE order_rules AS
c_max_items CONSTANT PLS_INTEGER := 100;
SUBTYPE quantity_t IS PLS_INTEGER RANGE 1..100;
TYPE item_t IS RECORD (
item_code VARCHAR2(30),
quantity quantity_t
);
TYPE item_list_t IS TABLE OF item_t INDEX BY PLS_INTEGER;
END order_rules;
/These types are PL/SQL types. They are useful in PL/SQL variables and subprogram parameters, but they are not automatically database table types that SQL statements can query.
A caller can declare a variable using a public record type:
DECLARE
l_item order_rules.item_t;
BEGIN
l_item.item_code := 'PEN-01';
l_item.quantity := 4;
DBMS_OUTPUT.PUT_LINE(l_item.item_code || ': ' || l_item.quantity);
END;
/The specification becomes the contract. If the package changes the fields in item_t, callers that depend on those fields may need recompilation or changes.
Public exceptions
A named exception gives a meaningful name to a failure condition. Declare it in the specification when callers need to handle that condition directly.
CREATE OR REPLACE PACKAGE order_rules AS
c_max_items CONSTANT PLS_INTEGER := 100;
e_quantity_invalid EXCEPTION;
PRAGMA EXCEPTION_INIT(e_quantity_invalid, -20001);
PROCEDURE validate_quantity(p_quantity IN PLS_INTEGER);
END order_rules;
/The PRAGMA EXCEPTION_INIT directive associates the exception with an Oracle error number. The package body can raise the exception, and callers can handle it with the qualified name.
CREATE OR REPLACE PACKAGE BODY order_rules AS
PROCEDURE validate_quantity(p_quantity IN PLS_INTEGER) IS
BEGIN
IF p_quantity < 1 OR p_quantity > c_max_items THEN
RAISE e_quantity_invalid;
END IF;
END validate_quantity;
END order_rules;
/BEGIN
order_rules.validate_quantity(150);
EXCEPTION
WHEN order_rules.e_quantity_invalid THEN
DBMS_OUTPUT.PUT_LINE('Quantity must be between 1 and 100.');
END;
/The exception is public, but its validation logic remains private to the body.
How public declarations connect
Choosing what to expose
| Declaration | Typical purpose | Public when |
|---|---|---|
| Constant | Shared limit or configuration value | Callers must use the same value |
| Subtype or record | Shared PL/SQL data shape | Callers pass or receive that shape |
| Exception | Recognizable business error | Callers must handle that error differently |
Public declarations also create dependencies. Changing a constant's value usually does not change the shape of the interface, but changing a record field or exception declaration can invalidate dependent PL/SQL objects.
Exercise
Should a package constant be declared in the specification or body if only the package's private procedures use it?
Show answer
Declare it in the body. It should be in the specification only when external callers need to reference it.