What is user define exception and example

Answer Posted / harinadh bolisetti

PL/SQL allows developers to define their own exceptions.
User can define the error/exception programmatically based on the business rule.

1. Define Exception
---------------------
We need to define the exception before we raise and handle. User Exceptions are defined using keyword EXCEPTION in declaration section of the block.
The syntax is as follows

<exception_name> EXCEPTION ;


2. Raise the Exception
--------------------------
Once the exceptions are defined , they need to be raised anywhere in the body depending upon predefined logic. User exceptions are raised using the keyword RAISE.
Syntax is as shown below

RAISE <exception_name>

3. Handle the Exception.
--------------------------
User exception are handled in the same way predefined exceptions are handled. They are handled in exception block using WHEN .. THEN keyword
Syntax is as shown below

WHEN <exception_name> THEN


example
----------------
DECLARE
low_sal EXCEPTION;
min_sal NUMBER:= 10000;
new_sal NUMBER:= 8000;
BEGIN
INSERT INTO EMP_EXC_DEMO(EMPNO, DEPTNO, SAL)
VALUES (4000,20,new_sal);
IF new_sal < min_sal THEN
RAISE low_sal;
END IF;
commit;
EXCEPTION
WHEN low_sal THEN
Rollback;
DBMS_OUTPUT.PUT_LINE ('Salary is less than '||min_sal);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE (SQLERRM);
END;

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is query cache in mysql? : Sql dba

577


What is optimistic concurrency control? : Transact sql

553


what is the difference between truncate and drop statements? : Sql dba

554


What is PL/SQL Records?

629


how to create a new table by selecting rows from another table in mysql? : Sql dba

563






What are inner outer left and right joins in sql?

519


What is delete command in sql?

558


How to use distinct and count in sql query? Explain

612


What is a primary key example?

515


What is the command used to fetch the first 5 characters of a string?

710


Can procedure in package be overloaded?

612


what is data control language? : Sql dba

588


How do I use google cloud in sql?

544


What are the sql versions?

522


What is the usage of nvl function?

597