Sub topic 8
Handling and raising errors in subprograms
Assume you know anonymous PL/SQL blocks, SELECT INTO, and basic procedure and function syntax. In a stored subprogram, an exception is an error condition that interrupts normal execution. The EXCEPTION section handles that condition.
Where exceptions are handled
A subprogram has three possible sections:
- A declaration section for variables and local exceptions.
- An executable section containing the main logic.
- An exception section containing handlers.
If no handler matches an exception, Oracle propagates it to the caller. The caller can handle it, or the error can reach the client application.
flowchart TD
A[Subprogram starts] --> B[Executable statements]
B --> C{Exception raised?}
C -->|No| D[Return normally]
C -->|Yes| E{Matching handler?}
E -->|Yes| F[Run handler]
E -->|No| G[Propagate to caller]Handling common exceptions
Oracle defines predefined exceptions such as NO_DATA_FOUND and TOO_MANY_ROWS. A SELECT INTO statement raises NO_DATA_FOUND when it finds no row and TOO_MANY_ROWS when it finds more than one row.
CREATE OR REPLACE FUNCTION get_full_name (
p_employee_id IN employees.employee_id%TYPE
) RETURN VARCHAR2 AS
v_name VARCHAR2(100);
BEGIN
SELECT first_name || ' ' || last_name
INTO v_name
FROM employees
WHERE employee_id = p_employee_id;
RETURN v_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
WHEN TOO_MANY_ROWS THEN
RAISE_APPLICATION_ERROR(
-20002,
'More than one employee has this ID'
);
END get_full_name;
/Returning NULL for a missing employee is a design choice. Another function might raise an application error instead. Choose behavior that makes the function's contract clear to its callers.
Raising application errors
Use RAISE_APPLICATION_ERROR when the input or database state violates a business rule. The error number must be between -20000 and -20999.
CREATE OR REPLACE PROCEDURE raise_salary (
p_employee_id IN employees.employee_id%TYPE,
p_percent IN NUMBER DEFAULT 10
) AS
BEGIN
IF p_percent < 0 THEN
RAISE_APPLICATION_ERROR(-20003, 'Percent cannot be negative');
END IF;
UPDATE employees
SET salary = salary * (1 + p_percent / 100)
WHERE employee_id = p_employee_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(
-20001,
'Employee not found: ' || p_employee_id
);
END IF;
END raise_salary;
/RAISE_APPLICATION_ERROR stops the current subprogram unless a surrounding handler catches the resulting error. It does not automatically commit or roll back the caller's transaction.
Catching and re-raising errors
A WHEN OTHERS handler matches every exception not handled earlier. Use it carefully. If you cannot recover, use bare RAISE to re-raise the same exception with its original error information.
CREATE OR REPLACE PROCEDURE safe_raise_salary (
p_employee_id IN employees.employee_id%TYPE,
p_percent IN NUMBER
) AS
BEGIN
raise_salary(p_employee_id, p_percent);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Salary update failed');
RAISE;
END safe_raise_salary;
/Put specific handlers before WHEN OTHERS. Do not use WHEN OTHERS THEN NULL; it hides failures and makes diagnosis difficult.
Exercise
Exercise
What happens when an UPDATE affects no rows in raise_salary?
Show answer
SQL%ROWCOUNT is zero, so the procedure raises application error -20001. The caller can catch or report that error.