Sub topic 2
Writing an EXCEPTION section with WHEN clauses
Assume that you know basic SQL, PL/SQL block structure, variables, and control structures. You should also understand that a transaction can be committed or rolled back.
A PL/SQL block has three optional parts:
- A
DECLAREsection for variables and local exceptions. - A
BEGINsection for executable statements. - An
EXCEPTIONsection for error handlers.
An exception is a runtime error or an explicitly raised condition. When an exception occurs in the executable section, PL/SQL stops the current statement and searches the EXCEPTION section for a matching WHEN clause.
Basic handler syntax
The EXCEPTION section comes after the executable statements and before the block's final END.
DECLARE
l_name employees.last_name%TYPE;
BEGIN
SELECT last_name
INTO l_name
FROM employees
WHERE employee_id = 9999;
DBMS_OUTPUT.PUT_LINE('Employee: ' || l_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee was found');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('More than one employee was found');
END;
/NO_DATA_FOUND and TOO_MANY_ROWS are predefined exceptions. Oracle raises NO_DATA_FOUND when the SELECT INTO statement returns no rows. It raises TOO_MANY_ROWS when the statement returns more than one row.
Only one handler runs. After a handler finishes, control leaves the current block. PL/SQL does not continue with the statement after the one that failed, and it does not try the next WHEN clause.
If no exception occurs, PL/SQL skips the entire EXCEPTION section.
How matching works
Each WHEN clause names an exception or exception group. Put specific handlers before a general handler.
flowchart TD
A[Execute statement] --> B{Did an exception occur?}
B -->|No| C[Skip EXCEPTION section]
B -->|Yes| D[Find matching WHEN clause]
D --> E[Run one handler]
E --> F[Leave the block]Handling several related errors
A single handler can name several predefined exceptions with OR.
BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = 100;
IF SQL%ROWCOUNT = 0 THEN
RAISE NO_DATA_FOUND;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND OR TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('The employee selection was not valid');
END;
/Use separate handlers when different errors need different responses. For example, a missing employee might be normal input, while multiple matching employees might indicate a data-quality problem.
The WHEN OTHERS handler
WHEN OTHERS matches any exception that was not matched by an earlier handler. It must be the last handler in the section.
BEGIN
DELETE FROM departments
WHERE department_id = 50;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No department was found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('The delete failed');
RAISE;
END;
/A WHEN OTHERS handler should usually record useful diagnostics and re-raise the exception with RAISE. Silently swallowing an unexpected error can make a failed operation look successful.
Exercise
What happens if a SELECT INTO raises NO_DATA_FOUND, and the matching handler prints a message?
Show answer
PL/SQL stops the failed statement, runs the NO_DATA_FOUND handler, and then leaves the block. It does not execute later statements in the BEGIN section.