User Defined Exceptions
User Defined Exceptions
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
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;