How to Raise Exception in PL/SQL?

You can raise an error in PL/SQL using user-defined exception.

Steps to Raise An Exception in PL/SQL

  1. Declare user-defined exception in the declare section of PL/SQL program unit.
  2. Raise it between the program upon some condition.
  3. 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:

  1. Create PDF reports in PL/SQL
  2. Raise application error example in PL/SQL
  3. How to create a Procedure inside a package in Oracle
  4. Exception Handling in PL/SQL
Get more Vinish.dev tutorials on Google

See my articles more often when you search Google.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE, author of four books on Oracle APEX, SQL and PL/SQL, and Oracle Forms, and a software developer building Oracle database applications since 2001.

guest

0 Comments
Oldest
Newest Most Voted