Anda di halaman 1dari 3

Exceptions These are used to handle the user errors i.

e, instead of entering number s if user enters a characters li e things then compiler chec s for handler i.e, Exception Handler and it will give the message accroding to handler. Exceptions Are Two Types: --> Built-In Exceptions --> User-defined Exceptions -> Built-in Exceptions : These Exceptions are used to handle errors that are a lready difined as a Errors. Syntax: Exception when <Exception_Name> Then Statements li e dml,drl and pl/sql statements; --> No_Data_Found : This Exception is raised when the select Statement is unabl e to fetch the data from DataBase. E.g. Declare E emp%rowtype; Begin select ename,empno into E.ename,E.empno from emp where empno=&no; dbms_output.put_line(E.ename ||' '||E.empno); Exception When No_Data_Found Then dbms_output.put_line('Record(s) Not Found'); End; ---------------------------------------------------------------------------------------------> Too_Many_Rows: This Exception is raised when Select statement fetches more than one record or value or row. E.g. Declare E emp%rowtype; Begin select ename,empno,sal into E.ename,E.empno ,E.sal from emp where sal=&salary; dbms_output.put_line(E.ename ||' '||E.empno| |' '||E.sal); Exception When Too_Many_Rows Then dbms_output.put_line('Too Many Rows Fetched'); End; Eg: More than one exception in a single bloc . Declare E Emp%Rowtype; C Char; Begin Select ename,empno into E.ename,E.empno from emp where ename li e('T%'); dbms_output.put_line(E.ename ||' '||E.empno); Select ename,empno into E.ename,E.empno from emp where deptno=&dno;

dbms_output.put_line(E.ename ||' '||E.empno); Exception When Too_Many_Rows Then dbms_output.put_line('Too Many Rows Fetched'); When No_Data_Found Then dbms_output.put_line('Record(s) Not Found'); End; / ------------------------------------------------------------> Value_Error: This Exception will be raised when the user is entering the va lues more than the size of the variable or a non-numeric value in numeric variab le. --> Zero-Divide: This Exception will be raised when a number is divided by Zer o. Declare m number(3):=&m; n number(3):=&n; Begin dbms_output.put_line('Quotient Of (m/n) Is='||m/n); Exception When Value_Error Then dbms_output.put_line('Enter Specific Digits and Numeric Values'); when Zero_Divide Then dbms_output.put_line('Zero Cannot Be Divided'); End; ------------------------------------------------------------> Dup_Val_On_Index : (Duplicate Value On Index) This Exception will be raised when user enter the duplicate values t o a Unique or Primary Key Column. --> Others : This Exception will be raised for any type of Errors. E.g. Declare E emp%rowtype; Begin E.empno:=&no; E.ename:=&name; E.sal :=&sal; insert into emp1 values(E.empno,E.ename,E.sal); commit; Exception When Dup_Val_On_Index Then dbms_output.put_line(' Employee No. Is Already Exist'); When Others Then dbms_output.put_line('Error Has Occured...'); dbms_output.put_line('Please Re-Chec The Code..!'); End; / ----------------------------------------------------------------------------------------------> User Defined Exceptions : These Exceptions raised and handled by the users.These can be declared in the Declaration Field. -> E.g. Declare E.g.

salary emp.sal%type; eno emp.empno%type; low_sal Exception; high_sal Exception; Begin Select sal into salary from emp where empno=&eno; if (salary<3000) then Raise low_sal; else Raise high_sal; end if; Exception when low_sal then Update emp Set sal=salary+500; where empno=eno; when high_sal then Update emp Set sal=salary+1000; where empno=eno; End; /

E.g. Declare ejob emp.job%type:=&ejob; Begin E.empno:=&no; E.ename:=&name; E.sal :=&sal; insert into emp1 values(E.empno,E.ename,E.sal); commit; Raise_Application dbms_output.put_line(' Employee No. Is Already Exist'); When Others Then dbms_output.put_line('Error Has Occured...'); dbms_output.put_line('Please Re-Chec The Code..!'); End; /

Anda mungkin juga menyukai