Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plsql/cursors handle exception and return back to the execution flow

I am trying to execute a cursor and want it to complete the loop even if there is some exception.

What I trying to do is "catch" all the exception and probably log something or do nothing and then return back to the flow . Here is how the code looks like:

 FOR line IN my_cursor
 LOOP
 begin
        
    if<condition> then
      GOTO pass; 
    else     
     <<do_something>>
     exception
       when others then
        sys.dbms_output.put_line('say something');       
    end if;  

    <<pass>> null;
 end
 END LOOP;

The script doesn't compile.

There is probably some syntax error with the exception, but I am also not aware of semantics very well. Like I am not sure if you can return back to execution flow after handling an exception.

p.s: The DB is 10g and there is not CONTINUE in it. Hence using GOTO.

like image 532
codeObserver Avatar asked Dec 19 '11 19:12

codeObserver


People also ask

What is usually returned by the cursor expression in Oracle?

A CURSOR expression returns a nested cursor. This form of expression is equivalent to the PL/SQL REF CURSOR and can be passed as a REF CURSOR argument to a function. A nested cursor is implicitly opened when the cursor expression is evaluated.

How do you continue a loop after an exception in PL SQL?

By putting a BEGIN-END block with an exception handler inside of a loop, you can continue executing the loop if some loop iterations raise exceptions. You can still handle an exception for a statement, then continue with the next statement. Place the statement in its own subblock with its own exception handlers.

What are the 4 cursor attributes used in PL SQL?

Each cursor has a set of attributes that enables an application program to test the state of the cursor. These attributes are %ISOPEN, %FOUND, %NOTFOUND, and %ROWCOUNT.

How would you handle exceptions in PL SQL and its types?

PL/SQL allows you to define your own exceptions according to the need of your program. A user-defined exception must be declared and then raised explicitly, using either a RAISE statement or the procedure DBMS_STANDARD.


1 Answers

Put the code that you want to execute within the loop in it's own block and then you can use that blocks exception section to handle any problems during the loop iteration.

Once the exception for that iteration is handled, the next loop iteration will start

e.g.:

for line in my_cursor
loop
   begin    
      <<do_something>>
   exception
      <<do_exception_processing>>        
   end;
end loop;

To illustrate this further, in the example below, I have declared a local variable of type exception. I am looping through numbers 1 to 10, during the second loop iteration, the if statement is true and processing passes to the exception handler. Once the exception is handled, the next iteration of the loop begins.

begin

   for i in 1 .. 10 
   loop

      declare

         my_exception exception;

      begin

         if i = 2
         then

            -- if you need to do some processing then you would enter it
            -- here and then when you want to enter the exception section 
            -- you would add the line below 

            raise my_exception;

         end if;

      exception
         when my_exception then
            dbms_output.put_line('in exception section');

      end;

   end loop;

end;
like image 141
Ian Carpenter Avatar answered Sep 23 '22 07:09

Ian Carpenter