0

In pl/sql I have some inner begin, end blocks and "Exception Others" blocks. When I throw my user defined exception from an inner block I just want to catch this exception in the last "Exception userdef" block, not in the inner "Exception Others" blocks. Any idea?

3
  • 3
    Why are you using WHEN OTHERS exception handlers? Why aren't you catching just the specific exceptions that you can actually handle? Commented Jun 14, 2013 at 13:01
  • Can you post the code you've tried? It will probably help explain your problem better. Commented Jun 14, 2013 at 14:20
  • @Justin, i already know some ways to solve the issue. I just wonder whether it is possible with less code or not. Commented Jun 15, 2013 at 13:19

2 Answers 2

3

It sounds like you have something like this:

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;

and you want to DO_SOMETHING_REASONABLE rather than DIE_HORRIBLY or DIE_EVEN_MORE_HORRIBLY. Sorry - you can't do that without providing a handler in the inner blocks for your exception. You'll have to do something like:

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;

Share and enjoy.

Sign up to request clarification or add additional context in comments.

2 Comments

You say just catch the exception in the inner block and throw again... I know that solution, I wonder if it is possible with less code...
Given the situation you described, where code in the inner-most block throws a user-defined exception, each nested block has a WHEN OTHERS handler, and you want to handle your user-defined exception only in the outer block, I can't think of another way to do this. I suppose in the inner WHEN OTHERS handlers you could check for your particular SQLCODE value and re-raise it, but I think that's even more of a kluge than what I suggested above. Basically, there's no way I know of to create a handler which will "Handle all errors except one I don't want handled". Share and enjoy.
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
*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.