Académique Documents
Professionnel Documents
Culture Documents
DECLARE
erreur EXCEPTION; ---- Déclaration exception
BEGIN
SELECT … INTO ……
DECLARE
nbre_emp INTEGER;
aucun_emp EXCEPTION; ---- Déclaration exception
BEGIN
SELECT COUNT(*) INTO nbre_emp FROM emp;
IF nbre_emp = 0 THEN
RAISE aucun_emp; ---- Levée exception
ELSE
DBMS_OUTPUT.PUT_LINE('La table EMP contient '||nbre_emp||' employes');
END IF;
EXCEPTION
WHEN aucun_emp THEN ---- Traitement exception
DBMS_OUTPUT.PUT_LINE('La table EMP est vide');
WHEN OTHERS THEN ---- Exception prédéfinie
DBMS_OUTPUT.PUT_LINE('Erreur inconnue ' || SQLERRM);
END;
/
Exemple de gestion d’exception
7
DECLARE
c INTEGER;
aucun_emp EXCEPTION; ---- Déclaration exception
BEGIN
SELECT COUNT(*) INTO c FROM emp;
IF c=0 THEN
RAISE aucun_emp; ---- Levée exception
………
EXCEPTION
WHEN aucun_emp THEN ---- Traitement exception
RAISE_APPLICATION_ERROR(-20501, 'La table EMP est vide');
………
END;
/
DECLARE
impot emp.sal%type;
net_payer emp.sal%type;
BEGIN
FOR emp_record IN (SELECT ename, sal, comm FROM emp)
LOOP
IF (emp_record.sal < 1000)
THEN impot := emp_record.sal * 0.25;
ELSIF emp_record.sal <= 1500
THEN impot := emp_record.sal * 0.3;
ELSE impot := emp_record.sal * 0.35;
END IF;
net_payer := emp_record.sal + nvl(emp_record.comm, 0) - impot;
DBMS_OUTPUT.PUT_LINE('L''employe '||emp_record.ename ||
' - Salaire = ' ||emp_record.sal|| ' - Impot = ' ||impot||' - Net a payer = ' ||net_payer);
END LOOP;
EXCEPTION
WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Erreur inconnue !!!');
END;
/