Question

I have other_procedure which can do a

RAISE_APPLICATION_ERROR(-20001,'Some text here');

When an error happens I want to jump to the end of the loop, which I do. But I don't know how to catch that error.

Please note that I'm using Oracle 10g, so I can't use continue so that's why I use GOTO.

I tried this, but with exception it won't compile.

create or replace procedure my_procedure
as    
begin
  for item
  IN some_cursor
  LOOP
    if(condition) then
      other_procedure; 
      exception when -20001 then
            goto end_loop;
      --some code here 
    end if;
    <<end_loop>>
    null; 
  END LOOP;
end;
Was it helpful?

Solution

IF you're trying to handle the custom error, then you'll have to bind it to a exception using PRAGMA EXCEPTION_INIT.

Another thing to note is that the exception you've attached is for your main procedure my_procedure, the compilation error is because the IF/LOOPs are still not closed in the body since exception signifies start of the exception handler

Here's how it should look

CREATE OR REPLACE PROCEDURE my_procedure
AS
   custom_exception   EXCEPTION;
   PRAGMA EXCEPTION_INIT (custom_exception, -20001); -- bind error code to custom_exception
BEGIN
   FOR item IN some_cursor
   LOOP
      IF (condition)
      THEN
         BEGIN
            other_procedure;
         EXCEPTION
            WHEN custom_exception -- handle error from other_procedure
            THEN
               GOTO end_loop;
         END;                                                 
      --some code here
      END IF;

     <<end_loop>>
      NULL;
   END LOOP;
END;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top