Sub topic 1
What Happens When a PL/SQL Block Hits an Error
Prerequisites
You should understand basic SQL statements, PL/SQL block structure, variables, and SELECT ... INTO. You should also know that a transaction groups database changes until COMMIT or ROLLBACK.
The normal execution path
A PL/SQL block runs its executable statements in order. A runtime error interrupts that order. PL/SQL represents the error as an exception, which is a condition that changes normal control flow.
When an exception occurs, PL/SQL stops executing the remaining statements in the current section. It then searches for an exception handler that matches the error. A handler is code in the EXCEPTION section that responds to a particular exception.
For example, this query finds no rows:
DECLARE
l_name VARCHAR2(100);
BEGIN
SELECT 'Ada'
INTO l_name
FROM dual
WHERE 1 = 0;
DBMS_OUTPUT.PUT_LINE('Name: ' || l_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('The query returned no rows');
END;
/The SELECT INTO statement raises the predefined exception NO_DATA_FOUND. The DBMS_OUTPUT call in the normal path never runs. Control moves directly to the matching WHEN clause.
How PL/SQL searches for a handler
A block can have several handlers. PL/SQL checks the handlers in the order written and runs the first matching one.
flowchart TD
A[Begin executable statements] --> B{Does a statement raise an exception?}
B -->|No| C[Continue with the next statement]
C --> B
B -->|Yes| D[Stop the current executable path]
D --> E{Matching handler in this block?}
E -->|Yes| F[Run the handler]
E -->|No| G[Propagate to the enclosing block]
F --> H[Leave the block]
G --> I{Matching handler in caller?}
I -->|Yes| F
I -->|No| J[Return the error to the client]The handler does not resume execution at the statement after the one that failed. After the handler finishes, control leaves that block. If no handler matches, the exception propagates to the enclosing block or to the program that called the block.
A handler can match a specific predefined exception:
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- Handle an empty SELECT INTO result
WHEN TOO_MANY_ROWS THEN
-- Handle a SELECT INTO result with multiple rows
END;TOO_MANY_ROWS occurs when a SELECT INTO statement expects one row but receives more than one. These two errors require different responses, so separate handlers make the intended behavior clear.
>callout note An exception does not automatically roll back every change made before the error. It changes control flow. Transaction actions such as COMMIT, ROLLBACK, and savepoint handling remain your responsibility.
A block without a matching handler
If a block has no matching handler, PL/SQL propagates the exception outward. The caller can handle it, log it, or return it to the client. Later chapters examine propagation and re-raising in detail.
Exercise
Which statement runs after the SELECT INTO raises NO_DATA_FOUND: the next normal statement, the matching WHEN clause, or both?
Show answer
The matching WHEN NO_DATA_FOUND clause runs. The next normal statement is skipped.