如何捕获其他程序,oracle SQL 10g中引发的过程中的错误

时间:2014-05-12 13:26:19

标签: sql oracle stored-procedures plsql

我有other_procedure可以做

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

当一个错误发生时,我想跳到循环的结尾,我这样做。但我不知道如何发现这个错误。

请注意我使用的是Oracle 10g,所以我不能继续使用,这就是我使用GOTO的原因。

我尝试了这个,但有例外它不会编译。

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;

1 个答案:

答案 0 :(得分:0)

如果您尝试处理自定义错误,那么您必须使用PRAGMA EXCEPTION_INIT将其绑定到异常。

另外需要注意的是,您附加的异常是针对您的主程序my_procedure,编译错误是因为IF / LOOP仍未在正文中关闭,因为异常表示异常处理程序的启动

以下是它的外观

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;