You can raise an error in PL/SQL using user-defined exception.
Steps to Raise An Exception in PL/SQL
- Declare user-defined exception in the declare section of PL/SQL program unit.
- Raise it between the program upon some condition.
- Handle it in the Exception section of PL/SQL program unit.
Example
SET SERVEROUTPUT ON; DECLARE /* first step */ v_error EXCEPTION; v_total NUMBER; BEGIN v_total := 2 + 2; IF v_total = 4 THEN /* second step */ RAISE v_error; END IF; DBMS_OUTPUT.put_line ('Total is not 4.'); EXCEPTION WHEN v_error THEN /* third step */ DBMS_OUTPUT.put_line ('Error: Total is 4.'); WHEN OTHERS THEN DBMS_OUTPUT.put_line ('Some error.'); END; /
Output
Error: Total is 4. PL/SQL procedure successfully completed.
Reference
User-defined Exception in Oracle
See also:
- Create PDF reports in PL/SQL
- Raise application error example in PL/SQL
- How to create a Procedure inside a package in Oracle
- Exception Handling in PL/SQL