Thursday, July 7, 2016

How to Create Exception in PL/SQL?

First Declare your exception name in Declaration section-
my_excp EXCEPTION

Then need to Raise your defined exception inside Begin section-
RAISE my_excp;

And last, Catch it in Exception section and and link the exception to a user-defined error number-
WHEN my_excp THEN
....

Below is the example of how to do-

DECLARE
my_excp EXCEPTION;
v_int1 NUMBER := 0;

BEGIN

IF v_int1 <> 3 THEN
RAISE my_excp;
END IF;

EXCEPTION

WHEN my_excp THEN
raise_application_error (-20009,'this is my exception');
END;

*** The Error number must be between -20000 and -20999

Note: DBMS_OUTPUT.PUT_LINE('test'); -- this will not work with raise_application_error



No comments:

Post a Comment