Connecting Tech Pros Worldwide Help | Site Map

EXCEPTIONS - 2

debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,505
#1   Jun 2 '07
Sample Example Of Zero_divide
===============================
Expand|Select|Wrap|Line Numbers
  1. Declare
  2. I Int:=&i;
  3. J Int:=&j;
  4. K Int;
  5. Begin
  6. K:=i/j;
  7. Dbms_output.put_line(k);
  8. Exception
  9. --if j=0 then (dividng the number by zero)
  10. When Zero_divide Then
  11. Raise_application_error(-20002,'cant Divide By Zero.....!');
  12. When Others Then
  13. Raise_application_error(-20003,'some Other Error........!');
  14. End;
  15.  
Zero_divide----This exception is raised when in any number id divided by zero in arithmetic operations.

Sample Example Of No_data_found
==============================
Expand|Select|Wrap|Line Numbers
  1. Declare
  2. Name Varchar2(20);
  3. No Int:=&no;
  4. Begin
  5. Select Ename Into Name From Emp Where Empno=no;
  6. Dbms_output.put_line(name);
  7. Exception
  8. When No_data_found Then
  9. Raise_application_error(-20002,'no Data Is Found For This Record.....!');
  10. When Others Then
  11. Raise_application_error(-20003,'some Other Error........!');
  12. End;
  13.  
No_data_found---This exception is raised whan any select into statment fails to retrive any data from database table. This may create eror in further data processing if left unhandled.

sample example of invalid_cursor
=============================
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2. CURSOR c1 IS SELECT ename FROM emp WHERE ROWNUM < 11;
  3. name emp.ename%TYPE;
  4. BEGIN
  5. OPEN c1;
  6. LOOP
  7. FETCH c1 INTO name;
  8. EXIT WHEN c1%NOTFOUND;
  9. dbms_output.put_line(c1%ROWCOUNT || '. ' || name);
  10. END LOOP;
  11. --cursor is closed
  12. CLOSE c1;
  13. --again trying to access the cursor variable after cursor is closed ,which raises an exception
  14. dbms_output.put_line(c1%ROWCOUNT); 
  15. exception
  16. when invalid_cursor then
  17. raise_application_error(-20001,'invalid operation in cursor');
  18. END;
  19.  
invalid_cursor--This explanation is self explanatory. Raises if there is any invalid cursor related operations.
Note --In the above example ,it raise the exception because the program tries to access the cursor variable %ROWCOUNT after the cursor is closed.

Sample Example Of DUP_VAL_ON_INDEX
==================================
Expand|Select|Wrap|Line Numbers
  1. create procedure insdept(num  number,name varchar2)
  2. is
  3. begin
  4. insert into dept(deptno,dname) values (num,name);
  5. commit;
  6. dbms_output.put_line('One row inserted...!');
  7. exception
  8. when dup_val_on_index then
  9. raise_application_error(-20001,'duplicate entry...!');
  10. when others then
  11. raise_application_error(-20002,'some other error occured...!');
  12. end;
  13.  
DUP_VAL_ON_INDEX-----This Exception is raised if a duplicate value is inserted on a primary key / unique field . Used to maintain data integrity.


Sample example showing TOO_MANY_ROWS
==================================
Expand|Select|Wrap|Line Numbers
  1. declare
  2. name varchar2(20);
  3. begin
  4. select ename into name from emp where deptno=&deptnumber;
  5. dbms_output.put_line(name);
  6. exception
  7. when NO_DATA_FOUND then
  8. raise_application_error(-20001,'no such data found...1');
  9. when TOO_MANY_ROWS then
  10. raise_application_error(-20002,'more than one matching record found...!');
  11. when OTHERS then
  12. raise_application_error(-20003,'some unexpected error occured...!');
  13. end;
  14.  
  15.  
TOO_MANY_ROWS---This exception is raised when more data is selected than that can be stored in the target variable in a select into statment.


Also check EXCEPTIONS - 3



Reply