adspace


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

How do I remove duplicates in two columns?

1202


how many tables will create when we create table, what are they? : Sql dba

1156


What is the current version of sql?

1106


how to use regular expression in pattern match conditions? : Sql dba

1125


what are all the common sql function? : Sql dba

1141


what is bcp? When does it used? : Sql dba

1065


Is primary key always clustered index?

1101


what is dbms? : Sql dba

1070


Does group by remove duplicates?

1120


What is your daily office routine?

2353


Hi am new to PLSQL & facing problems in writing code like in SP, Functions, so any one having some SP coding with in depth explanation please share with me my Email ID suvarnaatsuvarna@rediffmail.com Or taking tanning on this please do contact me

2102


how to escape special characters in sql statements? : Sql dba

1124


Can delete statement be rollbacked?

1065


Is primary key clustered index?

1028


define sql insert statement ? : Sql dba

1110