0% found this document useful (0 votes)
71 views4 pages

Ex No 8-EXCEPTION

The document contains examples of PL/SQL code to handle different predefined exceptions: 1) A block of code to handle the 'zero divide' exception by printing a message when an attempt is made to divide a number by 0. 2) A block of code to handle the 'no data found' exception when a row is not found by a SELECT INTO statement. 3) A block of code to handle a user defined 'invalid_eno' exception when a value greater than 35 is inserted into a table. 4) A block of code to handle the 'too many rows' exception when multiple rows are returned by a SELECT INTO statement.

Uploaded by

Elango Perumal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views4 pages

Ex No 8-EXCEPTION

The document contains examples of PL/SQL code to handle different predefined exceptions: 1) A block of code to handle the 'zero divide' exception by printing a message when an attempt is made to divide a number by 0. 2) A block of code to handle the 'no data found' exception when a row is not found by a SELECT INTO statement. 3) A block of code to handle a user defined 'invalid_eno' exception when a value greater than 35 is inserted into a table. 4) A block of code to handle the 'too many rows' exception when multiple rows are returned by a SELECT INTO statement.

Uploaded by

Elango Perumal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a PL/SQL to handle ‘zero divide’ predefined exception.

set serveroutput on
declare
a number:=&a;
b number:=&b;
c number;
begin
c:=a/b;
dbms_output.put_line(c);
exception
when zero_divide then
dbms_output.put_line('zero divide error');
end;
/
Output:
Enter value for a: 10
old 2: a number:=&a;
new 2: a number:=10;
Enter value for b: 5
old 3: b number:=&b;
new 3: b number:=5;
2
PL/SQL procedure successfully completed.
SQL> /
Enter value for a: 5
old 2: a number:=&a;
new 2: a number:=5;
Enter value for b: 0
old 3: b number:=&b;
new 3: b number:=0;
zero divide error
PL/SQL procedure successfully completed.
Write a PL/SQL to handle ‘no data found’ predefined exception
set serveroutput on
declare
e emp%rowtype;
begin
select * into e from emp where eno=&eno;
dbms_output.put_line('Eno name ');
dbms_output.put_line(e.eno||' '||e.name);
exception
when No_data_found then
dbms_output.put_line(' sorry friend, no data found');
end;
/
Enter value for eno: 12
old 4: select * into e from emp where eno=&eno;
new 4: select * into e from emp where eno=12;
Eno name
12 sam
PL/SQL procedure successfully completed.

SQL> /
Enter value for eno: 23
old 4: select * into e from emp where eno=&eno;
new 4: select * into e from emp where eno=23;
Eno name
23 madhu
PL/SQL procedure successfully completed.

SQL> /
Enter value for eno: 34
old 4: select * into e from emp where eno=&eno;
new 4: select * into e from emp where eno=34;
Eno name
34 bala
PL/SQL procedure successfully completed.

SQL> /
Enter value for eno: 13
old 4: select * into e from emp where eno=&eno;
new 4: select * into e from emp where eno=13;
sorry friend, no data found
PL/SQL procedure successfully completed.
Write a PL/SQL to handle user defined exception
set serveroutput on
declare
invalid_eno exception;
s emp.eno%type:=&eno;
begin
if s>35 then
raise invalid_eno;
else
insert into emp(eno)values(s);
dbms_output.put_line(' Record Inserted.....');
end if;
exception
when invalid_eno then
dbms_output.put_line('eno must be less than 35 ');
end;
/
Enter value for eno: 36
old 3: s emp.eno%type:=&eno;
new 3: s emp.eno%type:=36;
eno must be less than 35
PL/SQL procedure successfully completed.

SQL> /
Enter value for eno: 34
old 3: s emp.eno%type:=&eno;
new 3: s emp.eno%type:=34;
Record Inserted.....
PL/SQL procedure successfully completed.

SQL> /
Enter value for eno: 33
old 3: s emp.eno%type:=&eno;
new 3: s emp.eno%type:=33;
Record Inserted.....
PL/SQL procedure successfully completed.

SQL> /
Enter value for eno: 20
old 3: s emp.eno%type:=&eno;
new 3: s emp.eno%type:=20;
Record Inserted.....
PL/SQL procedure successfully completed.
SQL> select * from emp;
NAME ENO
---------- ----------
sam 12
madhu 23
bala 34
34
33
20
6 rows selected.

Write a PL/SQL to handle ‘Too many rows’ predefined exception.

SQL> create table emp(name varchar(10),eno number(5));


Table created.

SQL> insert into emp values('sam',12);


1 row created.
SQL> insert into emp values('madhu',23);
1 row created.
SQL> insert into emp values('bala',34);
1 row created.

SQL> select * from emp;


NAME ENO
---------- ----------
sam 12
madhu 23
bala 34

program:
set serveroutput on
declare
e emp %rowtype;
begin
select * into e from emp;
exception
when too_many_rows then
dbms_output.put_line('sorry,too many rows are selected');
end;
/
sorry,too many rows are selected
PL/SQL procedure successfully completed.

You might also like