Search code examples
plsqlplsql-package

How to handle plsql errors and exceptions


My code structure is as below.

PKG1

Procedure TEST1()
BEGIN
        PKG2.TEST2;
        PKG_X.TEST_X

EXCEPTION HANDLING

END;

PKG2

Procedure TEST2
BEGIN
    If(Condition is met)THEN
        //Should raise an error message
    END IF;

END;

A procedure(TEST1) in PKG1 calls a procedure(TEST2) in PKG2. There is an exception handling part in PKG1 but not in PKG2.

In TEST2, I want to raise an Error and stop the flow when a certain condition is met. When I debug the code it was noticed that the error message does not stop the flow. 'PKG_X.TEST_X' line is also executed. The error message can be seen in the Exception handling part and the debug code of TEST1 part. What would be the possible way to stop the flow while calling 'PKG2.TEST2'. Assume that the syntax and the functionality is correct.


Solution

  • You didn't post the important part: how exactly you raise an error? If you want to stop execution, then

    Procedure TEST2
    BEGIN
        If(Condition is met)THEN
            --Should raise an error message
            raise_application_error(-20000, 'Stop execution');       --> this
        END IF;    
    END;