What is user define exception and example



What is user define exception and example..

Answer / 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

More SQL PLSQL Interview Questions

i have a table with column sno with 30 records. i want to update this column by item by item in asp.net. i wantto enter new values into that from 1 to 30 how is it possible with backend c#

1 Answers  


What can you do with pl sql?

0 Answers  


What is sql architecture?

0 Answers  


how can we know the count/number of elements of an array? : Sql dba

0 Answers  


Is progress software supports to ( pl/sql )?

0 Answers  






What is partition in sql query?

0 Answers  


How do I find duplicates in two columns?

0 Answers  


What has stored procedures in sql?

0 Answers  


What is cascade in sql?

0 Answers  


if we give update table_name set column_name= default. what will happen? its given in pl sql block.what will hapen?

5 Answers   iFlex,


What is the difference between the implicit and explicit cursors?

0 Answers  


how to load data with out header and footer records in a database using sql*loader? pls tell me the answer urgently

1 Answers   Oracle, Wipro,


Categories