DECLARE
TYPE WORDLIST IS TABLE OF VARCHAR2(5);
WORDS WORDLIST;
ERR_MSG VARCHAR2(100);
PROCEDURE DISPLAY_ERROR
IS
BEGIN
ERR_MSG := SUBSTR(SQLERRM,1,100);
DBMS_OUTPUT.PUT_LINE('Error message = '
||ERR_MSG);
END;
BEGIN
BEGIN
WORDS(1) := 10; -- Raises COLLECTION_IS_NULL
-- A constructor has not been used yet.
-- Note: This exception applies to varrays and nested tables,
-- but not to associative arrays which do not need a constructor.
EXCEPTION
WHEN OTHERS THEN
DISPLAY_ERROR;
END;
-- After using a constructor, we can assign values to the elements.
WORDS := WORDLIST('1st','2nd','3rd'); -- 3 elements created
-- Any expression that returns a VARCHAR2(5) is valid.
WORDS(3) := WORDS(1)
||'+2';
BEGIN
WORDS(3) := 'longer than 5 characters'; -- Raises VALUE_ERROR
-- The assigned value is too long.
EXCEPTION
WHEN OTHERS THEN
DISPLAY_ERROR;
END;
BEGIN
WORDS('B') := 'dunno'; -- Raises VALUE_ERROR
-- The subscript (B) of a nested table must be an integer.
-- Note: Also, NULL is not allowed as a subscript.
EXCEPTION
WHEN OTHERS THEN
DISPLAY_ERROR;
END;
BEGIN
WORDS(0) := 'zero'; -- Raises SUBSCRIPT_OUTSIDE_LIMIT
-- Subscript 0 is outside the allowed subscript range.
EXCEPTION
WHEN OTHERS THEN
DISPLAY_ERROR;
END;
BEGIN
WORDS(4) := 'maybe'; -- Raises SUBSCRIPT_BEYOND_COUNT
-- The subscript (4) exceeds the number of elements in the table.
-- To add new elements, call the EXTEND method first.
EXCEPTION
WHEN OTHERS THEN
DISPLAY_ERROR;
END;
BEGIN
WORDS.DELETE(1);
IF WORDS(1) = 'First' THEN
NULL;
END IF; -- Raises NO_DATA_FOUND
-- The element with subcript (1) has been deleted.
EXCEPTION
WHEN OTHERS THEN
DISPLAY_ERROR;
END;
END;
/
Collection Exceptions
0 comments:
Post a Comment