Sub topic 6
Declaring Your Own Exceptions
This chapter assumes you know basic SQL, PL/SQL block structure, variables, IF statements, loops, stored procedures, and transaction basics.
Oracle provides predefined exceptions such as NO_DATA_FOUND. You can also declare an exception for a business rule that Oracle cannot detect automatically. A user-defined exception is a named condition that your PL/SQL code can raise and handle.
Declaring and raising an exception
Declare an exception in the declaration section of a block, procedure, or function. Then use RAISE when the invalid condition occurs.
DECLARE
e_invalid_discount EXCEPTION;
l_discount NUMBER := 120;
BEGIN
IF l_discount < 0 OR l_discount > 100 THEN
RAISE e_invalid_discount;
END IF;
DBMS_OUTPUT.PUT_LINE('Discount accepted');
EXCEPTION
WHEN e_invalid_discount THEN
DBMS_OUTPUT.PUT_LINE('Discount must be between 0 and 100');
END;
/The declaration gives the condition a name. The RAISE statement interrupts normal execution and transfers control to the matching WHEN clause. The statement after RAISE does not run.
A user-defined exception does not automatically include a useful message. The handler should provide the message, or the program can use RAISE_APPLICATION_ERROR when the caller needs an application error number and message.
Exception scope
An exception belongs to the block where it is declared. A nested block can handle an exception declared by its parent, but a parent block cannot directly name an exception declared only inside a child block.
DECLARE
e_invalid_amount EXCEPTION;
BEGIN
BEGIN
RAISE e_invalid_amount;
EXCEPTION
WHEN e_invalid_amount THEN
DBMS_OUTPUT.PUT_LINE('Handled inside the child block');
END;
END;
/If the inner block does not handle e_invalid_amount, the exception can propagate to the outer block because the outer block owns its declaration. If no enclosing block handles it, the exception reaches the caller.
A procedure can declare an exception locally and handle it there. If a caller must recognize the condition by name, declare the exception in a package specification. This gives both the procedure and its callers access to the same exception identifier.
Associating an Oracle error
Use PRAGMA EXCEPTION_INIT when you want a name for a specific Oracle error. The error number is negative.
DECLARE
e_child_exists EXCEPTION;
PRAGMA EXCEPTION_INIT(e_child_exists, -2292);
BEGIN
DELETE FROM departments
WHERE department_id = 50;
EXCEPTION
WHEN e_child_exists THEN
DBMS_OUTPUT.PUT_LINE('Dependent rows prevent this delete');
END;
/The pragma associates e_child_exists with ORA-02292. This is clearer than testing SQLCODE directly in every handler. Choose a name that describes the condition from the application’s point of view.
Exception flow
A handler can recover, record information, or re-raise the exception. Re-raising lets a higher-level caller decide what to do.
Exercise
What happens if a block raises a locally declared exception and has no matching handler?
Show answer
The exception propagates to an enclosing block or the calling program. If nothing handles it, PL/SQL reports an unhandled exception.