Navigation Bar

Saturday, April 20, 2024

Collections : Exceptions and Bulk Collect Exceptions

DECLARE

SUBSCRIPT_OUTSIDE_LIMIT  EXCEPTION;
PRAGMA  EXCEPTION_INIT(SUBSCRIPT_OUTSIDE_LIMIT, -06532);

TYPE tab_student_list IS VARRAY(5) OF VARCHAR2(100); 

vt_students   tab_student_list := tab_student_list(); 
BEGIN
   vt_students.EXTEND(5);
   vt_students(1) := 'AKSHAY';
   vt_students(2) := 'AMRIT';
   vt_students(3) := 'MAC';
   vt_students(4) := 'SUSHEEL';
   vt_students(5) := 'SHAIL';
   
   FOR i IN vt_students.FIRST .. vt_students.LAST
   LOOP --{
    dbms_output.put_line('Student # ' || i || ' :' || vt_students(i));
   END LOOP; --}
  
   vt_students.EXTEND;  --this will give error as we try to EXTEND the varray beyond 5 elements
   
EXCEPTION
  WHEN SUBSCRIPT_OUTSIDE_LIMIT THEN
    dbms_output.put_line('Cannot extend vt_students array beyond 5 elements');
END;
Below is the error
Error report -
ORA-06532: Subscript outside of limit
ORA-06512: at line 22
06532. 00000 -  "Subscript outside of limit"
*Cause:    A subscript was greater than the limit of a varray
           or non-positive for a varray or nested table.
*Action:   Check the program logic and increase the varray limit
           if necessary.
You can handle the exception using PRAGMA EXCEPTION_INIT and assigning the ORA-06532 code to an exception variable.
DECLARE 
SUBSCRIPT_OUTSIDE_LIMIT  EXCEPTION;
PRAGMA  EXCEPTION_INIT(SUBSCRIPT_OUTSIDE_LIMIT, -06532);		   
......
EXCEPTION
  WHEN SUBSCRIPT_OUTSIDE_LIMIT THEN
    dbms_output.put_line('Cannot extend vt_students array beyond 5 elements');
END;
Output is as below
Student # 1 :AKSHAY
Student # 2 :AMRIT
Student # 3 :MAC
Student # 4 :SUSHEEL
Student # 5 :SHAIL
Cannot extend vt_students array beyond 5 elements
For more details on excetion handling you can refer to Exception Handling Methods

Bulk Collect Exception Handling

There are good examples of bulk exception handling in below link

https://www.databasejournal.com/oracle/bulk-exceptions-in-oracle/

To continue ...


God's Word for the day
SELF-CONTROL
Do not follow your base desires, but restrain your appetites
  If you allow your soul to take pleasure in base desire,
It will make you the laughing stock of your enemies
  Do not revel in great luxury or you may become impoverished by its expense
Do not become a beggar by feasting with borrowed money
  When you have nothing in your purse.
The one who does this will not become rich
  one who despises small things will fail little by little.
Sirach 18:30-33

Jesus stills the storm 
And he said to them, "Why are you afraid you of little faith?"
  Then he got up and rebuked the winds and the sea; and there was a dead calm
Mathew 8:26

No comments:

Post a Comment