Poster un commentaire ou une réponse
TP Exception

nb posts:320
nb discussions:33
inscrit le :23-01-2015
exemple 1:exception oracle too_many_rows, le traitement est affichage d'un message d'erreur
declare
lname varchar2(15);
begin
select last_name into lname from employees where first_name='John';
dbms_output.put_line('John''s last name is:'||lname);
exception
when too_many_rows then
dbms_output.put_line('your select statement retrieved multiple rows.Consider using a cursor.');
end;
exemple 2: deux exceptions too_many_rows et no_data_found, le traitement est affichage d'un message d'erreur
declare
lname varchar2(15);
begin
select last_name into lname from employees where last_name='John';
dbms_output.put_line('John''s last name is:'||lname);
exception
when too_many_rows then
dbms_output.put_line('your select statement rtrieved many rows...');
when no_data_found then
dbms_output.put_line('no data found corresponding to your select statement');
end;
exemple 3:deux exceptions too_many_rows et no_data_found, le traitement est insertion du message d'erreur dans une table
à créer
create table t11
(c1 varchar(20));
declare
lname varchar2(15);
begin
select last_name into lname from employees where last_name='John';
dbms_output.put_line('John''s last name is:'||lname);
exception
when too_many_rows then
insert into t11 values('trop de lignes');
when no_data_found then
insert into t11('no data found');
end;
exemple 4:le traitement d'autres exceptions "others"ici est d'insérer dans une table à créer le code d'erreur ainsi que
le message d'erreur retournés par sqlcode et sqlerrm
create table errors(ec number,em varchar2(255));
declare
lname varchar2(15);
error_code number;
error_message varchar2(255);
begin
select last_name into lname from employees where last_name='John';
dbms_output.put_line('John''s last name is:'||lname);
exception
when too_many_rows then
dbms_output.put_line('your select statement rtrieved many rows...');
when others then
error_code:=sqlcode;
error_message:=sqlerrm;
insert into errors values (error_code,error_message);
end;
correction de cet exemple proposé par le prof:
create table tab1(error_code number,error_message varchar2(255));
declare
var employees.last_name%type;
begin
select last_name into var from employees where last_name='John';
exception
when too_many_rows then
dbms_output.put_line('trop de lignes');
when others then
insert into tab1 values(sqlcode,sqlerrm);
end;
exemple5:ajout d'une exception définie par l'utilisateur qui sera déclenchée si v1>v2 pour afficher le message d'erreur
v1 est supérieur à v2
declare
var employees.last_name%type;
v1 number:=50;
v2 number:=30;
sup_exception exception;
begin
select last_name into var from employees where last_name='John';
if v1>v2 then
raise sup_exception;
end if;
exception
when too_many_rows then
dbms_output.put_line('trop de lignes');
when sup_exception then
dbms_output.put_line('v1 est supérieur à v2');
when others then
insert into tab1 values(sqlcode,sqlerrm);
exemple6: exception définie par user qui sera déclenchée si le nombre de lignes pour le curseur est sup à 30
declare
las varchar2(20);
cursor emp_cur is select last_name from employees;
i number;
verif exception;
begin i:=1;
open emp_cur;
fetch emp_cur into las;
while emp_cur%found loop
fetch emp_cur into las;
i:=i+1;
dbms_output.put_line(las);
if i>30 then
raise verif;
end if;
exit when emp_cur%notfound;
end loop;
exception
when verif then
dbms_output.put_line('nombre de lignes sup à 30');
end;
exemple 7: exception avec affichage ORA-20000:v1 sup à v2
declare
v1 number:=50;
v2 number:=30;
begin
if v1>v2 then
raise_application_error(-20000,'v1 sup à v2');
end if;
end;
exemple 8: on va affecter à une exception définie par user un numéro
cas 1:affectation de numéro+traitement est l'affichage d'un message d'erreur personnalisé
declare
v1 number:=50;
v2 number:=30;
supp exception;
pragma exception_init(supp,-20000);
begin
if v1>v2 then
raise supp;
end if;
exception
when supp then
dbms_output.put_line('v1 sup à v2');
end;
cas 2:affectation de numéro+traitement est l'affichage du msg ORA-20000:erreur
declare
v1 number:=50;
v2 number:=30;
supp exception;
pragma exception_init(supp,-20000);
begin
if v1>v2 then
raise_application_error(-20000,'erreur');
end if;
exception
when supp then
dbms_output.put_line('sup');
end;
Poster |