Sub topic 3
How Exceptions Propagate Through Nested Blocks and Calls
This chapter assumes you know PL/SQL block structure, the EXCEPTION section, predefined exceptions such as NODATAFOUND, and the COMMIT and ROLLBACK statements.
Propagation Through Nested Blocks and Calls
When an exception is raised, PL/SQL checks the EXCEPTION section of the current block for a matching WHEN clause. If none matches, the exception moves to the enclosing block. If it leaves a called procedure unhandled, it moves to the caller.
DECLARE
e_bad_input EXCEPTION;
BEGIN
BEGIN
RAISE e_bad_input;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Inner handler');
END;
DBMS_OUTPUT.PUT_LINE('Skipped');
EXCEPTION
WHEN e_bad_input THEN
DBMS_OUTPUT.PUT_LINE('Caught in outer block');
END;
/Errors in Declarations and Handlers
An error raised in a declaration section, such as a DEFAULT expression that divides by zero, is not handled by that block's own EXCEPTION section. It moves to the enclosing block. An exception raised inside a WHEN handler also skips the other handlers in the same section and moves outward.
Capturing the Error Stack and Backtrace
Call DBMSUTILITY.FORMATERRORSTACK to get the message chain. Call DBMSUTILITY.FORMATERRORBACKTRACE to get the line where the error started. The backtrace still shows that origin after the exception has crossed several blocks.
Exceptions, Rollback, and Savepoints
A handler does not undo work. Changes made before an exception stay pending until the caller issues ROLLBACK. To undo only part of the work, set a SAVEPOINT before the risky step, then run ROLLBACK TO savepoint_name in the handler.
Logging Errors That Survive a Rollback
Mark a logging procedure with PRAGMA AUTONOMOUS_TRANSACTION and commit inside it. Its row is saved even if the caller later rolls back.
Bulk DML Errors with FORALL SAVE EXCEPTIONS
With SAVE EXCEPTIONS, FORALL runs every iteration. Afterward, PL/SQL raises ORA-24381. SQL%BULKEXCEPTIONS lists each failure, with ERRORINDEX giving the failed iteration and ERROR_CODE giving the Oracle error number.
DECLARE
TYPE t_divisors IS TABLE OF NUMBER;
l_divisors t_divisors := t_divisors(2, 0, 5);
e_bulk_errors EXCEPTION;
PRAGMA EXCEPTION_INIT(e_bulk_errors, -24381);
BEGIN
FORALL i IN 1 .. l_divisors.COUNT SAVE EXCEPTIONS
UPDATE employees SET salary = salary / l_divisors(i)
WHERE department_id = 50;
EXCEPTION
WHEN e_bulk_errors THEN
FOR j IN 1 .. SQL%BULK_EXCEPTIONS.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('Iteration ' || SQL%BULK_EXCEPTIONS(j).ERROR_INDEX ||
': ' || SQLERRM(-SQL%BULK_EXCEPTIONS(j).ERROR_CODE));
END LOOP;
ROLLBACK;
END;
/Guidelines and Common Mistakes
- Do not use WHEN OTHERS without logging or re-raising. Otherwise the error disappears.
- Do not assume a handler rolls back changes.
- Use RAISEAPPLICATIONERROR with numbers from -20000 to -20999 for custom errors.
- Wrap per-row work in an inner block so one failure does not end a loop.
Exercise
A helper procedure raises an unhandled ZERO_DIVIDE. The caller ran an UPDATE just before calling it and has no handler. Is that UPDATE undone when the exception reaches the caller?
Show answer
No. PL/SQL does not undo it. The UPDATE stays uncommitted until someone issues ROLLBACK, so the caller or the client must decide whether to roll back.