| use defined exception [message #563813] |
Fri, 17 August 2012 05:53  |
ajaykumarkona
Messages: 261 Registered: August 2010
|
Senior Member |
|
|
Hi Experts,
Can any body please tell me when we use user defined exception.
Please provide one business scenario for user defined exception.
Please help me.
Thanks in advance.
|
|
|
|
| Re: use defined exception [message #563833 is a reply to message #563813] |
Fri, 17 August 2012 06:46   |
ThomasG
Messages: 2881 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
You use a user defined exception when you want to pass an exception (for example from a procedure stored in the database) to the calling program (for example your user GUI frontend) that isn't a "standard" exception.
For example the frontend could call a "cancel this document" procedure in the DB, and the DB could raise an "the document is already cancelled" exception that is then displayed by the frontend.
|
|
|
|
| Re: use defined exception [message #563834 is a reply to message #563833] |
Fri, 17 August 2012 06:54   |
 |
Littlefoot
Messages: 16928 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Here's a (stupid) example based on Scott's schema; I want that ALL employees have a manager.
SQL> create or replace trigger trg_biu_emp
2 before insert or update
3 on emp
4 for each row
5 declare
6 exc_no_mgr exception;
7 begin
8 if :new.mgr is null then
9 raise exc_no_mgr;
10 end if;
11
12 exception
13 when exc_no_mgr then
14 raise_application_error(-20001, 'You have to specify employee''s manager');
15 end;
16 /
Trigger created.
SQL> insert into emp (empno, ename) values (99, 'Littlefoot');
insert into emp (empno, ename) values (99, 'Littlefoot')
*
ERROR at line 1:
ORA-20001: You have to specify employee's manager
ORA-06512: at "SCOTT.TRG_BIU_EMP", line 10
ORA-04088: error during execution of trigger 'SCOTT.TRG_BIU_EMP'
SQL> select empno, ename from emp where mgr is null;
EMPNO ENAME
---------- ----------
7839 KING
SQL> update emp set comm = 1000 where ename = 'KING';
update emp set comm = 1000 where ename = 'KING'
*
ERROR at line 1:
ORA-20001: You have to specify employee's manager
ORA-06512: at "SCOTT.TRG_BIU_EMP", line 10
ORA-04088: error during execution of trigger 'SCOTT.TRG_BIU_EMP'
SQL>
|
|
|
|
|
|
| Re: use defined exception [message #563839 is a reply to message #563836] |
Fri, 17 August 2012 07:24   |
ThomasG
Messages: 2881 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
I always thought about sending back non-standard-codes with raise_application_error as one way to do "user defined exceptions" quickly.
After all, it raises an exception back to the calling procedures that I have "defined" (at least in the locial sense, if not in the code-sense of defining it in code and using PRAGMA EXCEPTION_INIT to assign it a number.
|
|
|
|
|
|