473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

EXCEPTIONS - 3

debasisdas
8,127 Recognized Expert Expert
EXAMPLE SHOWING USE OF USER DEFINED EXCEPTION
==============================================
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2. M NUMBER(4);
  3. MYERROR EXCEPTION;
  4. BEGIN
  5. SELECT COMM INTO M FROM EMP WHERE EMPNO=7788;
  6. IF M IS NULL THEN
  7. RAISE MYERROR;
  8. END IF;
  9. EXCEPTION
  10. WHEN NO_DATA_FOUND THEN
  11. DBMS_OUTPUT.PUT_LINE('ERROR OF DATA......!');
  12. WHEN TOO_MANY_ROWS THEN
  13. DBMS_OUTPUT.PUT_LINE('ERROR OF TOOMANY ROWS.....!');
  14. WHEN MYERROR THEN
  15. DBMS_OUTPUT.PUT_LINE('ERROR FOUND NULL......!');
  16. END;
  17.  
USER DEFINED EXCEPTION----Used to raise an exception explicitely. Must be declared in the declaration section and handled in the expeption block.

EXAMPLE SHOWING USE OF USER DEFINED EXCEPTION
==============================================
Expand|Select|Wrap|Line Numbers
  1.  DECLARE
  2.  SALARY EMP.SAL%TYPE;
  3.  NAME EMP.ENAME%TYPE;
  4.  NO NUMBER:=&NO;
  5.  GREATER EXCEPTION;   --User defined exception
  6.  LESSER EXCEPTION;    --User defined exception
  7.  BEGIN
  8.  SELECT ENAME,SAL INTO NAME,SALARY FROM EMP WHERE  EMPNO=NO;
  9.  IF SALARY>2000 THEN
  10.  RAISE GREATER;   --User defined exception raised explicitely.
  11.  ELSE
  12.  RAISE LESSER;     --User defined exception raised explicitely.
  13.  END IF;
  14.  EXCEPTION
  15.  WHEN GREATER THEN
  16.  RAISE_APPLICATION_ERROR(-20001,'YOUR SALARY IS MORE  THAN 2000');
  17. --User defined exception handled here.
  18.  WHEN LESSER THEN
  19.  RAISE_APPLICATION_ERROR(-20002,'YOUR SALARY IS LESS   THAN 2000');
  20. --User defined exception handled here.
  21.  WHEN NO_DATA_FOUND THEN
  22.  RAISE_APPLICATION_ERROR(-20003,'PLEASE   ENTER A VALID  EMPNO');
  23.  END;
  24.  
NOTE--Both predeined and userdefined exceptions can be handled in a single exception block.

USER DEFINED EXCEPTION FOR ORACLE DEFINED NUMBER
=================================================
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2. EXP1 EXCEPTION;
  3. PRAGMA EXCEPTION_INIT(EXP1,-00001);   --exp1 is initialized to error number 00001
  4. BEGIN
  5. INSERT INTO DEPT VALUES(&DNO,'&DNAME','&LOC');
  6. DBMS_OUTPUT.PUT_LINE('ONE RECORD INSERTED');
  7. EXCEPTION
  8. WHEN EXP1 THEN
  9. DBMS_OUTPUT.PUT_LINE('DUPLICATE VALUE');
  10. --when the exception 00001 is raised instead of showing oracle defined message user defined message is displayed.
  11. END;
  12.  
  13.  
A pragma is a compiler directive that is processed at compile time, not at run time.
The pragma must appear somewhere after the exception declaration in the same declarative section.


Also check EXCEPTIONS - 4
Jun 2 '07 #1
0 3684

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: David Turner | last post by:
Hi all I noticed something interesting while testing some RAII concepts ported from C++ in Python. I haven't managed to find any information about it on the web, hence this post. The problem...
21
by: dkcpub | last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq about this. And I fished around in the IDLE menus but didn't see anything. Is there a tool that can determine all the...
26
by: OvErboRed | last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding checked exceptions (the longest-running of which seems to be <cJQQ9.4419 $j94.834878@news02.tsnz.net>. My personal...
9
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned...
6
by: RepStat | last post by:
I've read that it is best not to use exceptions willy-nilly for stupid purposes as they can be a major performance hit if they are thrown. But is it a performance hit to use a try..catch..finally...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.