Sub topic 11
Errors Raised in Declarations and Inside Handlers
Assume you know PL/SQL block structure, predefined exceptions, and basic EXCEPTION sections. This chapter focuses on two boundaries that often surprise developers: the declaration section and an exception handler.
Exceptions raised during declarations
A PL/SQL block has three parts:
- A declaration section.
- An executable section.
- An exception-handling section.
The block's EXCEPTION section handles errors raised in its executable section. However, an error raised while declaring or initializing a variable occurs before that block's handler is active. The exception must propagate to an enclosing block.
BEGIN
DECLARE
l_value NUMBER := 1 / 0;
BEGIN
DBMS_OUTPUT.PUT_LINE(l_value);
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Inner handler');
END;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Outer handler caught the declaration error');
END;
/The assignment to l_value runs during the inner block's declaration phase. The inner ZERO_DIVIDE handler cannot catch it. Control skips the unfinished inner block and moves to the outer handler.
The same rule applies when a declaration calls a function, queries data, or evaluates another expression that raises an exception.
Exceptions raised inside handlers
A handler is not protected by the other handlers in the same EXCEPTION section. If a handler raises a new exception, PL/SQL leaves the current block and searches for a handler in an enclosing block.
BEGIN
BEGIN
RAISE NO_DATA_FOUND;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Handling the missing row');
DBMS_OUTPUT.PUT_LINE(1 / 0);
END;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Outer handler caught the handler error');
END;
/The inner block catches NO_DATA_FOUND. While running that handler, 1 / 0 raises ZERO_DIVIDE. PL/SQL does not restart the inner EXCEPTION section and does not test its WHEN clauses again. The new exception propagates to the outer block.
| Where the exception is raised | Handler that can catch it |
|---|---|
| Inner executable section | A matching handler in the inner block |
| Inner declaration section | A matching handler in an enclosing block |
| Inner handler | A matching handler in an enclosing block |
Designing safe boundaries
Use an enclosing block when initialization or error-reporting code can fail. You can also split risky work into a nested block with a deliberately small handler:
BEGIN
BEGIN
-- Risky work belongs here.
RAISE VALUE_ERROR;
EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('Expected input problem');
END;
DBMS_OUTPUT.PUT_LINE('Outer block continues');
END;
/If the handler itself raises an error, the outer block must decide whether to handle it, log it, or allow it to reach the caller. Do not assume that WHEN OTHERS in the same block will catch a later error from another handler.
Exercise
Which block catches an exception raised while a variable is initialized in an inner block, and which block catches an exception raised inside the inner handler?
Show answer
In both cases, a matching handler in an enclosing block catches the exception. The inner block's exception section is not re-entered.