Sub topic 3
Handling Predefined Exceptions Such as NO_DATA_FOUND
Assume you know basic SQL, PL/SQL block structure, variables, loops, stored procedures, and transaction commands. This chapter focuses on predefined exceptions: named Oracle errors that PL/SQL can raise automatically.
How predefined exceptions work
An exception is a runtime error that interrupts normal execution. When a PL/SQL statement raises an exception, control moves immediately to the EXCEPTION section of the current block. PL/SQL searches the WHEN clauses for a matching exception name.
A SELECT INTO statement expects exactly one row:
- If it finds no rows, Oracle raises
NO_DATA_FOUND. - If it finds more than one row, Oracle raises
TOO_MANY_ROWS. - If it finds one row, it assigns the selected values and continues.
The statement that raised the exception does not finish, and statements after it in the BEGIN section are skipped.
Writing handlers for exact-row queries
The following block handles both common outcomes. The employees table is assumed to contain an employee_id column and a last_name column.
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 with that ID');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('More than one employee matched');
END;
/NO_DATA_FOUND and TOO_MANY_ROWS are predefined exceptions. You do not declare them before using them. Oracle declares them in the standard package, so their names are available throughout PL/SQL.
A handler can also use values from variables that were assigned before the error. However, do not assume that the target variable from a failed SELECT INTO contains a useful value. The assignment might not have completed.
Choosing the right query shape
Use SELECT INTO when the data model or condition guarantees one row. A primary-key lookup is a typical example. If several rows are valid, use a cursor loop or another query that does not require one exact result.
If no row is a normal outcome, a cursor loop can avoid treating it as an error:
BEGIN
FOR r IN (
SELECT employee_id, last_name
FROM employees
WHERE department_id = 999
) LOOP
DBMS_OUTPUT.PUT_LINE(r.employee_id || ': ' || r.last_name);
END LOOP;
END;
/This loop runs zero times when no employees match. It does not raise NO_DATA_FOUND.
>``callout note >Do not use WHEN OTHERS when you specifically expect NODATAFOUND or TOOMANYROWS. Named handlers document the expected condition and prevent unrelated errors from being mistaken for normal query outcomes. >``
Common mistakes
- Assuming
SELECT INTOreturnsNULLwhen no row matches. It raisesNO_DATA_FOUND. - Handling only
NO_DATA_FOUNDwhen duplicate data can produceTOO_MANY_ROWS. - Writing output after a failing statement and expecting it to run.
- Using an exact-row query when the business rule permits multiple rows.
- Catching an exception and silently continuing without recording or reporting the problem.
Exercise
What exception does a SELECT INTO statement raise when three rows match its condition, and which handler should process it?
Show answer
It raises TOO_MANY_ROWS, so a WHEN TOO_MANY_ROWS THEN handler should process it.