zundarz
zundarz

Reputation: 1594

How to continue PL/SQL excution beyond exceptions?

This exceptions processing article suggests putting the EXCEPTIONS clause inside the nested block. When the exception arises then proceed with the next record.

It works in this OP.

However, I get a PLS-000103 Error when I try this.

Here's my code:

QUESTION

How do I continue processing records (A,C,E) that are not exceptions?

set serveroutput on;
/* Goal is to process all records that don't cause exceptions.
Using guidance from 
http://www.oracle.com/technetwork/issue-archive/2011/11-mar/o29plsql-085126.html
Desired results
===============
A
C
E
*/

declare
  v_exception varchar2(1);
  v_item_a    varchar2(2);
  v_item_b    varchar2(1);

  cursor c_list
  is 
  (
  select 'A'  as item from dual union all
  select 'BB' as item from dual union all
  select 'C' as item from dual union all
  select 'DD' as item from dual union all
  select 'E' as item from dual union all
  select 'FF' as item from dual);

begin

  for items in c_list loop
     v_item_b := items.item;
     dbms_output.put_line(v_item_b);
     /* Putting an excpetion block inside the for loop causes
        PLS-00103: Encountered the symbol "EXCEPTION" 

       exception when others
       then dbms_output.put_line('Msg 28:[' || SQLERRM || ']');
       end;
      */ 
  end loop;
     /* Putting it here works.         
     */
  exception when others
  then dbms_output.put_line('Exception OK here:[' || SQLERRM || ']');
  end;

Upvotes: 0

Views: 3041

Answers (1)

doberkofler
doberkofler

Reputation: 10431

Try a block (and generally avoid WHEN OTHERS)

BEGIN
   <the statement that can raise the exception>
EXCEPTION
   WHEN OTHERS THEN
      ...
END;

Upvotes: 1

Related Questions