0

Could somebody please confirm for me that if I'm running a PLSQL script containing multiple blocks of code and I capture an error - does using the 'RAISE' command on it's own skip through to the exception handling at the end of the script or will it continue to run through the remaining blocks of code? Alternatively if I raise the exeception using 'RAISE handle_tns_error' where handle_tns_error is defined as an exception and also listed in the exception section at the end (as in the example below), will that skip past all other blocks in the code and exit immediately with an error? Thank you.

.
.
.

  ELSIF SQLCODE = -12154 THEN
     DBMS_OUTPUT.PUT_LINE('Detected a TNS error');
     RAISE handle_tns_error;
  ELSE
     RAISE;
  END IF;
END;

<all code here will be ignored>

EXCEPTION
    WHEN handle_tns_error THEN
        DBMS_OUTPUT.PUT_LINE('tns error detected');
        :ret_code := 1;
    WHEN OTHERS THEN RAISE;
END;
/
1
  • By the way, handle_tns_error seems an odd name for an exception. We don't have handle_no_data_found or handle_too_many_rows. Handling is what the code does with the exception. The exception isn't its own handler. Commented Jul 3, 2021 at 10:06

3 Answers 3

2

Raise within the execution block should always be followed by user defined exception or system defined exception otherwise you will get the below error:

PLS-00367: a RAISE statement with no exception name must be inside an exception handler

Raise without name exception should be inside exception block.

Now 2nd part of your question is regarding user defined exception. In this case yes all other code will be skipped and control will pass to the exception block when this user defined exception handler code is written.

1
  • Thank you for that answer.
    – Franco
    Commented Jun 30, 2021 at 20:29
2

If you raise an exception, the handler at the end of the block will catch it. If the block has no exception handler, it will be handled by an outer block and in the end by the client.

2
  • Thanks. Appreciate the feeback.
    – Franco
    Commented Jun 30, 2021 at 20:29
  • @Franco This is why WHEN OTHERS THEN RAISE; is not useful. It was going to do that anyway, and with a better error stack. Commented Jul 3, 2021 at 10:07
1

Throwing an Exception is a two-stage process (which is why using it for "normal" program flow-control is generally frowned-upon).

  • In the first step, the run-time scans back up through the Call Stack, trying to find a handler for the Exception being thrown.
  • Having found an Exception handler, the run-time then unwinds the Call Stack until it can transfer control to the Exception handler.

So yes, throwing an exception that the current block doesn't have a handler for will bypass the code in the rest of the current block and any [other] handlers that it might have.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.