忽略oracle中的用户定义的异常

时间:2013-06-14 12:55:31

标签: oracle exception plsql defined

在pl / sql中,我有一些内部开头,结束块和“Exception Others”块。 当我从内部块中抛出用户定义的异常时,我只想在最后一个“Exception userdef”块中捕获此异常,而不是在内部的“Exception Others”块中。 有什么想法吗?

2 个答案:

答案 0 :(得分:3)

听起来你有这样的事情:

BEGIN
  BEGIN
    BEGIN
      DO_SOMETHING;  -- raises USERDEF_EXCEPTION 
    EXCEPTION
      WHEN OTHERS THEN
        DIE_HORRIBLY;
    END;
  EXCEPTION
    WHEN OTHERS THEN
      DIE_EVEN_MORE_HORRIBLY;
  END;
EXCEPTION
  WHEN USERDEF_EXCEPTION THEN
    DO_SOMETHING_REASONABLE;
  WHEN OTHERS THEN
    DIE_INCREDIBLY_HORRIBLY;
END;

你想要DO_SOMETHING_REASONABLE而不是DIE_HORRIBLY或DIE_EVEN_MORE_HORRIBLY。抱歉 - 如果不在内部块中为异常提供处理程序,则无法执行此操作。你必须做类似的事情:

BEGIN
  BEGIN
    BEGIN
      DO_SOMETHING;  -- raises USERDEF_EXCEPTION 
    EXCEPTION
      WHEN USERDEF_EXCEPTION THEN
        RAISE;
      WHEN OTHERS THEN
        DIE_HORRIBLY;
    END;
  EXCEPTION
    WHEN USERDEF_EXCEPTION THEN
      RAISE;
    WHEN OTHERS THEN
      DIE_EVEN_MORE_HORRIBLY;
  END;
EXCEPTION
  WHEN USERDEF_EXCEPTION THEN
    DO_SOMETHING_REASONABLE;
  WHEN OTHERS THEN
    DIE_INCREDIBLY_HORRIBLY;
END;

分享并享受。

答案 1 :(得分:0)

/* package */
CREATE OR REPLACE PACKAGE exceptions_pkg AS
    user_defined_exception EXCEPTION;
END exceptions_pkg;

/* block */
DECLARE
    l_var1 NUMBER;
BEGIN
    DBMS_OUTPUT.PUT_LINE('one');

    DECLARE
        l_var2 NUMBER;
    BEGIN
        DBMS_OUTPUT.PUT_LINE('two');

        IF 1 < 2 THEN
            RAISE exceptions_pkg.user_defined_exception;
        END IF;

        DBMS_OUTPUT.PUT_LINE('three');
    END;

    DBMS_OUTPUT.PUT_LINE('four');
EXCEPTION
    WHEN exceptions_pkg.user_defined_exception THEN
        DBMS_OUTPUT.PUT_LINE('five');
END;

-- anonymous block completed
/*
one
two
five
*/