0% found this document useful (0 votes)
1 views19 pages

Exceptions Fonctions Procédure PLSQL RS

The document discusses exception handling in PL/SQL, including how to declare, intercept, and manage exceptions using predefined and user-defined error types. It provides examples of handling specific exceptions and demonstrates the use of procedures and functions to manage errors effectively. Additionally, it outlines the syntax for creating stored procedures and functions, as well as how to call them.

Uploaded by

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

Exceptions Fonctions Procédure PLSQL RS

The document discusses exception handling in PL/SQL, including how to declare, intercept, and manage exceptions using predefined and user-defined error types. It provides examples of handling specific exceptions and demonstrates the use of procedures and functions to manage errors effectively. Additionally, it outlines the syntax for creating stored procedures and functions, as well as how to call them.

Uploaded by

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

Generated by Foxit PDF Creator © Foxit Software

http://www.foxitsoftware.com For evaluation only.

Traiter les exceptions

Réalisé par Dr. Rihab SAID

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Exemple d'exception
DECLARE
v_lname VARCHAR2(15);
BEGIN
SELECT last_name INTO v_lname
FROM employees
WHERE first_name='John';
DBMS_OUTPUT.PUT_LINE ('John''s last name is :'
||v_lname);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE (' Your select statement
retrieved multiple rows. Consider using a
cursor.');
END;
/

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Traiter les exceptions en langage PL/SQL


• Une exception est une erreur PL/SQL détectée pendant l'exécution du
programme.
• Une exception peut être générée :
• implicitement par le serveur Oracle
• explicitement par le programme
• Une exception peut être traitée :
• par interception à l'aide d'un gestionnaire
• par propagation vers l'environnement appelant

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Intercepter les exceptions


•Syntaxe :

EXCEPTION
WHEN exception1 [OR exception2 . . .] THEN
statement1;
statement2;
. . .
[WHEN exception3 [OR exception4 . . .] THEN
statement1;
statement2;
. . .]
[WHEN OTHERS THEN
statement1;
statement2;
. . .]

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Règles d'interception des exceptions


• Le mot-clé EXCEPTION débute la section de traitement
des exceptions.
• Plusieurs gestionnaires d'exceptions sont autorisés.
• Un seul gestionnaire est traité avant la sortie du bloc.
• WHEN OTHERS est la dernière clause.

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Intercepter les erreurs prédéfinies


du serveur Oracle
• Utilisez le nom prédéfini à l'intérieur du sous-programme
de traitement des exceptions.
• Exemples d'exceptions prédéfinies :
• NO_DATA_FOUND
• TOO_MANY_ROWS
• INVALID_CURSOR
• ZERO_DIVIDE
• DUP_VAL_ON_INDEX

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Erreur non prédéfinie


•Intercepter l'erreur numéro –01400 du serveur Oracle
("impossible d'insérer la valeur NULL")
DECLARE
e_insert_excep EXCEPTION; 1
PRAGMA EXCEPTION_INIT(e_insert_excep, -01400); 2
BEGIN
INSERT INTO departments
(department_id, department_name) VALUES (280, NULL);
EXCEPTION 3
WHEN e_insert_excep THEN
DBMS_OUTPUT.PUT_LINE('INSERT OPERATION FAILED');
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Fonctions d'interception des exceptions


•Exemple
DECLARE
error_code NUMBER;
error_message VARCHAR2(255);
BEGIN
...
EXCEPTION
...
WHEN OTHERS THEN
ROLLBACK;
error_code := SQLCODE ;
error_message := SQLERRM ;
INSERT INTO errors (e_user, e_date, error_code,
error_message) VALUES(USER,SYSDATE,error_code,
error_message);
END;
/

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Intercepter les exceptions définies par


l'utilisateur
DECLARE
v_deptno NUMBER := 500;
v_name VARCHAR2(20) := 'Testing';
e_invalid_department EXCEPTION;
BEGIN
1
UPDATE departments
SET department_name = v_name
WHERE department_id = v_deptno;
IF SQL % NOTFOUND THEN
RAISE e_invalid_department;
2
END IF;
COMMIT;
EXCEPTION 3
WHEN e_invalid_department THEN
DBMS_OUTPUT.PUT_LINE('No such department id.');
END;
/

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Procédure RAISE_APPLICATION_ERROR
•Syntaxe :
•raise_application_error (error_number,
• message[, {TRUE | FALSE}]);
• Vous pouvez utiliser cette procédure pour générer
des messages d'erreur définis par l'utilisateur à partir
de sous-programmes stockés.
• Elle permet de signaler les erreurs à l'application et
d'éviter le renvoi d'exceptions non traitées.

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Procédure RAISE_APPLICATION_ERROR
•Section exécutable :
•BEGIN
•...
DELETE FROM employees
• WHERE manager_id = v_mgr;
IF SQL%NOTFOUND THEN
• RAISE_APPLICATION_ERROR(-20202,
'This is not a valid manager');
• END IF;
• ...
•Section de traitement des exceptions :
...
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR (-20201,
'Manager is not a valid employee.');
END;
/

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Créer des procédures


et des fonctions stockées

Réalisé par Dr. Rihab SAID

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Procédure : Syntaxe

CREATE [OR REPLACE] PROCEDURE procedure_name


[(argument1 [mode1] datatype1,
argument2 [mode2] datatype2,
. . .)]
IS|AS
procedure_body;

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Procédure : Exemple

...
CREATE TABLE dept AS SELECT * FROM departments;
CREATE PROCEDURE add_dept IS
v_dept_id dept.department_id%TYPE;
v_dept_name dept.department_name%TYPE;
BEGIN
v_dept_id:=280;
v_dept_name:='ST-Curriculum';
INSERT INTO dept(department_id,department_name)
VALUES(v_dept_id,v_dept_name);
DBMS_OUTPUT.PUT_LINE(' Inserted '|| SQL%ROWCOUNT
||' row ');
END;
Dr. Rihab Said
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Appeler la procédure

BEGIN
add_dept;
END;
/
SELECT department_id, department_name FROM dept
WHERE department_id=280;

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Fonction : Syntaxe

CREATE [OR REPLACE] FUNCTION function_name


[(argument1 [mode1] datatype1,
argument2 [mode2] datatype2,
. . .)]
RETURN datatype
IS|AS
function_body;

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Fonction : Exemple
CREATE FUNCTION check_sal RETURN Boolean IS
v_dept_id employees.department_id%TYPE;
v_empno employees.employee_id%TYPE;
v_sal employees.salary%TYPE;
v_avg_sal employees.salary%TYPE;
BEGIN
v_empno:=205;
SELECT salary,department_id INTO v_sal,v_dept_id FROM
employees
WHERE employee_id = v_empno;
SELECT avg(salary) INTO v_avg_sal FROM employees WHERE
department_id=v_dept_id;
IF v_sal > v_avg_sal THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Appeler une fonction

BEGIN
IF (check_sal IS NULL) THEN
DBMS_OUTPUT.PUT_LINE('The function returned
NULL due to exception');
ELSIF (check_sal) THEN
DBMS_OUTPUT.PUT_LINE('Salary > average');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary < average');
END IF;
END;
/

Dr. Rihab Said


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Transmettre un paramètre à une fonction


DROP FUNCTION check_sal;
CREATE FUNCTION check_sal(p_empno employees.employee_id%TYPE)
RETURN Boolean IS
v_dept_id employees.department_id%TYPE;
v_sal employees.salary%TYPE;
v_avg_sal employees.salary%TYPE;
BEGIN
SELECT salary,department_id INTO v_sal,v_dept_id FROM employees
WHERE employee_id=p_empno;
SELECT avg(salary) INTO v_avg_sal FROM employees
WHERE department_id=v_dept_id;
IF v_sal > v_avg_sal THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
EXCEPTION
...

Dr. Rihab Said

You might also like