Sub topic 10
How Exceptions Propagate Through Nested Blocks and Calls
This chapter assumes that you know PL/SQL block structure, stored procedures, and basic transaction control. The key rule is simple: when an exception is raised, PL/SQL searches for a matching handler in the current block. If it finds none, the exception moves outward to the enclosing block or to the program that called the subprogram.
Propagation through nested blocks
A nested block is a BEGIN ... END block inside another block. If the inner block has a matching handler, it handles the error. Execution then continues with the statement after the inner block.
BEGIN
DBMS_OUTPUT.PUT_LINE('Before inner block');
BEGIN
RAISE VALUE_ERROR;
EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('Inner handler ran');
END;
DBMS_OUTPUT.PUT_LINE('After inner block');
END;
/The output includes both Inner handler ran and After inner block. PL/SQL does not return to the statement that raised the exception. It leaves the inner block after its handler finishes.
If the inner block has no matching handler, the exception propagates to the outer block.
BEGIN
BEGIN
RAISE VALUE_ERROR;
END;
DBMS_OUTPUT.PUT_LINE('This statement is skipped');
EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('Outer handler ran');
END;
/The exception prevents the statement after the inner block from running. The outer handler receives the exception instead.
Propagation through procedure calls
A procedure call creates another execution boundary, but it does not automatically consume exceptions. An exception that is not handled inside the procedure propagates to the calling block.
CREATE OR REPLACE PROCEDURE check_employee (
p_employee_id IN employees.employee_id%TYPE
) IS
l_name employees.last_name%TYPE;
BEGIN
SELECT last_name
INTO l_name
FROM employees
WHERE employee_id = p_employee_id;
DBMS_OUTPUT.PUT_LINE(l_name);
END check_employee;
/If the query finds no row, NO_DATA_FOUND leaves check_employee and reaches the caller:
BEGIN
check_employee(9999);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('The caller handled the missing employee');
END;
/A handler in the procedure can consume the exception instead. In that case, the caller does not receive it unless the procedure explicitly uses RAISE to re-raise it.
Scope and common mistakes
An exception declared inside a procedure is local to that procedure. A caller cannot name that exception directly unless the exception is declared in a shared package specification. For errors identified by Oracle error numbers, use PRAGMA EXCEPTION_INIT or handle the error in the procedure and raise a suitable application error.
Avoid adding WHEN OTHERS THEN NULL. It hides failures and makes propagation impossible to diagnose. Either handle a known, recoverable condition or record useful details and re-raise the exception.
Exercise
In a nested block, what happens to a NO_DATA_FOUND exception when the inner block has no matching handler but the outer block does?
Show answer
The exception leaves the inner block, skips the remaining statements in that block's execution path, and is handled by the matching handler in the outer block.