0% found this document useful (0 votes)
32 views

User Defined Exceptions

User Defined Exceptions

Uploaded by

muhammad.danish
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

User Defined Exceptions

User Defined Exceptions

Uploaded by

muhammad.danish
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Database Programming Using Oracle

11g

User Defined
Exceptions
User Defined Exceptions
What is User
Defined
Exception
• User defined
exceptions are
defined by
user.
• User Defined
exceptions are
raised by user
as per control
of the program
• At time
User Defined Exceptions

Syntax of User
Defined
Exceptions
User Defined Exceptions
DECLARE
user_define_exception_name EXCEPTION;
BEGIN
statement(s);
IF condition THEN
RAISE user_define_exception_name;
END IF;
EXCEPTION
WHEN user_define_exception_name THEN
User defined statement (action) will be
taken; END;
User Defined Exceptions

Implementing
User Defined
Exception - I
User Defined Exceptions
declare
Invalid_salary exception;
name emp.ename%type;
salary emp.sal%type;
begin
select ename, sal into name, salary
from emp where empno=7369;
if salary <5000 then
raise invalid_salary;
else
User Defined Exceptions
dbms_output.put_line('Valid
Salary');
end if;
exception
when invalid_salary then
dbms_output.put_line('InValid
Salary');
end;
User Defined Exceptions

Implementing
User Defined
Exception - II
User Defined Exceptions

Write a PL/SQL block to insert row


in emp table, while inserting data
in emp table if salary is less than
minimum salary then raise an
exception other
User Defined Exceptions
DECLARE
low_sal EXCEPTION;
min_sal NUMBER:= 10000;
new_sal NUMBER:= 8000;
BEGIN
INSERT INTO EMP(EMPNO,
DEPTNO, SAL)
VALUES (4000,20,new_sal);
IF new_sal < min_sal THEN
RAISE low_sal;
END IF;
User Defined Exceptions

EXCEPTION
WHEN low_sal THEN
DBMS_OUTPUT.PUT_LINE ('Salary is
less than '||min_sal);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE
(SQLERRM);
END;
User Defined Exceptions

Implementing
User Defined
Exception - III
User Defined Exceptions
DECLARE
past_due EXCEPTION;
acct_num NUMBER;
BEGIN
DECLARE -- sub-block begins

acct_num NUMBER;
due_date DATE := SYSDATE - 1;
todays_date DATE := SYSDATE;
User Defined Exceptions
BEGIN
IF due_date < todays_date THEN
RAISE past_due; -- this is not
handled
END IF;
END; -- sub-block ends
EXCEPTION
-- Does not handle raised
exception
WHEN past_due THEN
User Defined Exceptions
DBMS_OUTPUT.PUT_LINE ('Handling
PAST_DUE exception in Global
declaration.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE
('Could not recognize
PAST_DUE_EXCEPTION in this
scope.' || SQLCODE);
END;

You might also like