Sub topic 4
Catching Everything with `WHEN OTHERS`
Assume you know how to write PL/SQL blocks, declare variables, and use predefined exception handlers. In PL/SQL, WHEN OTHERS is a handler that matches any exception not matched by an earlier WHEN clause.
Use it as a safety net, not as a replacement for specific handlers. A specific handler explains the expected failure. WHEN OTHERS handles unexpected failures and usually records diagnostic details before re-raising the exception.
How `WHEN OTHERS` works
Handlers are checked from top to bottom. Oracle runs the first matching handler and skips the remaining handlers. Put specific exceptions before WHEN OTHERS.
DECLARE
l_name employees.last_name%TYPE;
BEGIN
SELECT last_name
INTO l_name
FROM employees
WHERE employee_id = 9999;
DBMS_OUTPUT.PUT_LINE(l_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee was not found');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Several employees matched');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error: ' || SQLERRM);
RAISE;
END;
/NO_DATA_FOUND and TOO_MANY_ROWS handle known results of the query. WHEN OTHERS handles another error, such as a conversion error or a database constraint error.
WHEN OTHERS must be the final handler. No handler can follow it.
Inspecting the unexpected error
Inside a WHEN OTHERS handler:
SQLCODEreturns the numeric error code.SQLERRMreturns the error message.RAISEre-raises the exception currently being handled.
BEGIN
UPDATE employees
SET salary = salary / 0
WHERE employee_id = 100;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Code: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE('Message: ' || SQLERRM);
RAISE;
END;
/The assignment raises an error. The handler prints information and then re-raises the same exception. A calling application can therefore detect the failure and decide whether to roll back or show an error.
Do not replace every error with a generic success message. This makes failed work look successful and makes debugging difficult. If you need to add business context, print or log it, then re-raise.
Handling errors inside a loop
A handler in the outer block ends the whole block when it runs. To continue after one failed item, put a nested block inside the loop.
BEGIN
FOR r IN (
SELECT employee_id
FROM employees
WHERE department_id = 50
) LOOP
BEGIN
UPDATE employees
SET salary = salary / 0
WHERE employee_id = r.employee_id;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(
'Skipped employee ' || r.employee_id || ': ' || SQLERRM
);
END;
END LOOP;
END;
/Here, the inner handler catches each failure, so the loop can process the next employee. This pattern is appropriate only when skipping a failed item is intentional. Otherwise, re-raise the exception.
Practical rule
Use specific handlers for errors your program expects. Use WHEN OTHERS to capture unexpected errors, preserve diagnostic information, and re-raise unless the program has a deliberate recovery strategy.
Exercise
Why should RAISE usually appear at the end of a WHEN OTHERS handler?
Show answer
It preserves the original failure and lets the caller respond to it instead of making the failed block appear successful.