Collection 异常
示例 4:演示 Collection 异常
演示各种 PL/SQL 预定义的 collection 异常,备注的部分说明如何避免这些异常。
DECLARETYPE WordList IS TABLE OF VARCHAR2(5);
words WordList;
err_msg VARCHAR2(100);
PROCEDURE display_error IS
BEGINerr_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. EXCEPTIONWHEN OTHERS THEN display_error;
END;-- After using a constructor, you 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'; BEGINwords(3) := 'longer than 5 characters'; -- Raises VALUE_ERROR
-- The assigned value is too long. EXCEPTIONWHEN OTHERS THEN display_error;
END; BEGINwords('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. EXCEPTIONWHEN OTHERS THEN display_error;
END; BEGINwords(0) := 'zero'; -- Raises SUBSCRIPT_OUTSIDE_LIMIT
-- Subscript 0 is outside the allowed subscript range. EXCEPTIONWHEN OTHERS THEN display_error;
END; BEGINwords(4) := 'maybe'; -- Raises SUBSCRIPT_BEYOND_COUNT
-- The subscript (4) exceeds the number of elements in the table.-- To add new elements, invoke the EXTEND method first. EXCEPTIONWHEN 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) was deleted. EXCEPTIONWHEN OTHERS THEN display_error;
END;END;/
在子块处理产生的异常,执行就会继续。下面是一些异常:
| Collection 异常 | 当……产生…… |
|
COLLECTION_IS_NULL |
尝试操作一个自动为 null 的 collection |
|
NO_DATA_FOUND |
一个标值指定了一个被删除的元素,或一个 associative array 不存在的元素 |
|
SUBSCRIPT_BEYOND_COUNT |
一个标值超过了一个 collection 元素的数量 |
|
SUBSCRIPT_OUTSIDE_LIMIT |
一个标值超出了被允许的范围 |
|
VALUE_ERROR |
一个标值为 null 或不能转换成键的类型。若键被定义为 PLS_INTEGER,或标值超出范围,则会产生该异常 |
在多数情况下,你可以传递一个不可靠的标值给方法,而不会产生异常。例如,当你传递一个 null 给 DELETE(n) 时,该方法什么都不会做。给已经删除的元素赋值,来替换这个已删除的元素,不会产生 NO_DATA_FOUND 异常。
示例 5:演示 DELETE(n) 如何处理不可靠的标值
DECLARETYPE NumList IS TABLE OF NUMBER;
nums NumList := NumList(10,20,30); -- initialize tableBEGINnums.DELETE(-1); -- does not raise SUBSCRIPT_OUTSIDE_LIMIT
nums.DELETE(3); -- delete 3rd element
DBMS_OUTPUT.PUT_LINE(nums.COUNT); -- prints 2
nums(3) := 30; -- allowed; does not raise NO_DATA_FOUNDDBMS_OUTPUT.PUT_LINE(nums.COUNT); -- prints 3
END;/
示例 6:演示包和本地 Collection 之间不兼容性
包的 collection 类型与本地 collection 类型不兼容。例如,若你调用包的存储过程,如下所示,第二个存储过程会调用失败,因为,包的和本地的 varray 类型不兼容,尽管它们具有相同的定义。
CREATE PACKAGE pkg AS
TYPE NumList IS TABLE OF NUMBER;
PROCEDURE print_numlist (nums NumList);END pkg;/
CREATE PACKAGE BODY pkg AS
PROCEDURE print_numlist (nums NumList) IS
BEGINFOR i IN nums.FIRST..nums.LAST LOOP
DBMS_OUTPUT.PUT_LINE(nums(i));
END LOOP; END;END pkg;/
DECLARETYPE NumList IS TABLE OF NUMBER;
n1 pkg.NumList := pkg.NumList(2,4); -- type from the package. n2 NumList := NumList(6,8); -- local type.BEGIN pkg.print_numlist(n1); -- type from pkg is legal-- The packaged procedure cannot accept-- a value of the local type (n2)-- pkg.print_numlist(n2); -- Causes a compilation error.END;/
