Você está na página 1de 9

EXCEPTIONS

1).PREDEFINED EXCEPTIONS
declare
ename varchar2(20);
esal number(10);
begin
select first_name,salary into ename,esal from employees
where employee_id=100;
dbms_output.put_line(ename||esal);
exception
when no_data_found then
dbms_output.put_line('noooooooo');
when too_many_rows then
dbms_output.put_line('sssss');
when others then
dbms_output.put_line('dddddd');
end;
/
2).NON-PREDEFINED EXCEPTION
declare
my_exception exception;
pragma_exception_init(my_exception,-01400);
begin
insert into tab values(1,'india');
insert into tab values(2,'us');
insert into tab values(3,null);
exception
when zero_divide then
dbms_output.put_line('all errrr');
when my_exception then
dbms_output.put_line('cannot insert null values');
end;
/
select * from tab

In exceptions, both pre-defined and non pre-defined are


implicitly invoked by the oracle.
In pre-defined exceptions we just refer the exception in
the exception block.
In non pre-defined exception we name the exception and
associate the error number with the name through
pragma exception_init which in turn directs the compiler
to the exception section and refer the named exception
to process further.

EXCEPTION PROPAGATION

Exceptions are also raised in

Declaration Section

Executable Section

Exception Section

If the exceptions are raised in


executable section those exceptions are handled
using either inner block or an outer block.
Eg:
begin
declare
a number(2):=12;
begin
a:=123;

dbms_output.put_line(a);
exception
when value_error then
dbms_output.put_line('size exceeded');
end;
exception
when value_error then
dbms_output.put_line('pls check size');
end;
Eg:2
begin
declare
a number(2):=12;
begin
a:=123;
dbms_output.put_line(a);
end;
exception
when value_error then
dbms_output.put_line('pls check size');
end;
/

Where as if exception are raised in


declaration section or in exception section those
exceptions are handled using outer blocks only.
Eg:
begin
declare
a number(2):=123;
begin

a:=12;
dbms_output.put_line(a);
exception
when value_error then
dbms_output.put_line('size exceeded');
end;
exception
when value_error then
dbms_output.put_line('pls check size');
end;
/
User Defined Exception with Raise Keyword
declare
x number(2):=&x;
y number(2):=&y;
z number(2);
b exception;
begin
z:=x+y;
dbms_output.put_line(z);
if z>30 then
raise b;
end if;
exception
when b then
dbms_output.put_line('it is a user defined
exception');
end;
When Executed without exception block
declare

x number(2):=&x;
y number(2):=&y;
z number(2);
b exception;
begin
z:=x+y;
dbms_output.put_line(z);
if z>30 then
raise b;
end if;
end;
Output:
Unhandled user define Exception
RAISING Predefined Exception
1) declare
vsal emp.salary%type;
a exception;
begin
select salary into vsal from emp where
employee_id=105;
if vsal>15000 then
raise a;
else
update emp set salary=vsal+3500 where
employee_id=105;
end if;
exception
when a then
dbms_output.put_line('size exceed');
end;
/
select * from emp

2)
declare
v emp%rowtype;
cursor c1 is select * into v from emp where
employee_id=103;
begin
open c1;
fetch c1 into v;
if c1%found then
dbms_output.put_line(v.first_name||''||
v.last_name||''||v.salary);
else
raise no_data_found;
end if;
exception
when no_data_found then
dbms_output.put_line('emp not valid no');
end;
/
3)
declare
v emp%rowtype;
cursor c1 is select * into v from emp where
job_id='IT_PROg';
begin
open c1;
fetch c1 into v;
if c1%notfound then
raise no_data_found;
else

dbms_output.put_line(v.first_name||''||v.salary);
end if;
close c1;
exception
when no_data_found then
dbms_output.put_line('job not valid id');
end;
/
EXCEPTION Raised in Exception section
declare
a1 exception;
a2 exception;
begin
begin
raise a1;
exception
when a1 then
dbms_output.put_line('a1 handled');
raise a2;
end;
exception
when a2 then
dbms_output.put_line('a2 handled');
end;
/
ERROR TRAPPING FUNCTION
declare
vsal emp%rowtype;
begin
select * into vsal from emp where
employee_id=103;

dbms_output.put_line(vsal.first_name||''||
vsal.salary);
dbms_output.put_line(sqlcode);
dbms_output.put_line(sqlerrm);
end;
/
OUTPUT:
Alexander 12468 0
ORA-0000: normal, successful completion
Statement processed.
0 means successful completed
RAISE_APPLICATION_ERROR
declare
vsal emp%rowtype;
a exception;
begin
select * into vsal from emp where
employee_id=103;
if vsal.salary>17000 then
raise a;
else
update emp set salary=vsal.salary+7000 where
employee_id=103;
dbms_output.put_line('updated successfuly');
end if;
exception
when a then
raise_application_error(-20100,'salary is high');
end;
/
OUTPUT

ORA-20100:salary is high
Suppose we can give
Raise_application_error(20100,'salary is high');
ORA-21000: error number argument to
raise_application_error of 20100 is out of range

Error Number: Ranges between -20000 to -20999


Error Msg: 512 Characters
L9VMZAMBFGTRAEG

Você também pode gostar