Sub topic 9
Returning Custom Errors with RAISE_APPLICATION_ERROR
Assume you know basic PL/SQL blocks, stored procedures, IF statements, DML, and transaction commands such as COMMIT and ROLLBACK. This chapter shows how to report business-rule failures to calling code.
Why use custom errors?
Oracle raises predefined exceptions for common failures, such as NO_DATA_FOUND or TOO_MANY_ROWS. Your application also needs to report failures that are specific to its rules. Examples include:
- A salary percentage is not positive.
- A requested customer is inactive.
- An employee does not belong to the required department.
- An order cannot be canceled after shipment.
RAISE_APPLICATION_ERROR creates an Oracle error with your message. It stops the current procedure or block unless an enclosing handler catches the error.
Syntax and error numbers
The basic syntax is:
RAISE_APPLICATION_ERROR(error_number, message [, keep_errors]);| Argument | Meaning |
|---|---|
| `error_number` | An integer from `-20000` through `-20999` |
| `message` | Text returned to the caller, up to 2,048 bytes |
| `keep_errors` | Optional Boolean that can preserve earlier errors in the error stack |
The error number identifies the application error. Give different business rules different numbers so callers can respond appropriately.
A complete procedure
This procedure validates its input, updates one employee, and reports two different failures:
CREATE OR REPLACE PROCEDURE raise_salary (
p_emp_id IN employees.employee_id%TYPE,
p_pct IN NUMBER
) IS
BEGIN
IF p_pct <= 0 THEN
RAISE_APPLICATION_ERROR(
-20001,
'Percentage must be positive'
);
END IF;
UPDATE employees
SET salary = salary * (1 + p_pct / 100)
WHERE employee_id = p_emp_id;
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(
-20002,
'Employee ' || p_emp_id || ' not found'
);
END IF;
END raise_salary;
/If the caller supplies -5, Oracle returns error ORA-20001. If no employee matches the ID, it returns ORA-20002.
The update happens before the second error is raised. In the same session, the caller should decide whether to commit or roll back the transaction. Raising an error does not automatically commit the update or roll back every change in the session.
Handling the custom error
A caller can catch all errors with WHEN OTHERS, inspect the error, and decide what to do:
BEGIN
raise_salary(100, -5);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Code: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE('Message: ' || SQLERRM);
ROLLBACK;
END;
/For larger systems, define a documented error-number convention. Keep messages useful to developers and safe for users. Do not include passwords, private data, or unnecessary internal details.
Reraising versus replacing
A handler can catch an error and raise a new application error:
BEGIN
DELETE FROM departments
WHERE department_id = 50;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(
-20010,
'Department could not be deleted: ' || SQLERRM
);
END;
/This gives callers a stable application error, but it can hide the original error location and stack details. Use this approach when the boundary needs a clear, stable contract. Otherwise, log the diagnostics and use plain RAISE to re-raise the original exception.
Exercise
What error number should you use for an application-defined PL/SQL error, and what happens if no enclosing handler catches it?
Show answer
Use a number from -20000 through -20999. The error leaves the procedure and is returned to the caller, where the surrounding statement or client can handle it.