Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  Oracle Corporation, 1999. All rights reserved. 23 Handling Exceptions.

Similar presentations


Presentation on theme: "Copyright  Oracle Corporation, 1999. All rights reserved. 23 Handling Exceptions."— Presentation transcript:

1 Copyright  Oracle Corporation, 1999. All rights reserved. 23 Handling Exceptions

2 23-2 Copyright  Oracle Corporation, 1999. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Define PL/SQL exceptions Recognize unhandled exceptions List and use different types of PL/SQL exception handlers Trap unanticipated errors Describe the effect of exception propagation in nested blocks Customize PL/SQL exception messages After completing this lesson, you should be able to do the following: Define PL/SQL exceptions Recognize unhandled exceptions List and use different types of PL/SQL exception handlers Trap unanticipated errors Describe the effect of exception propagation in nested blocks Customize PL/SQL exception messages

3 23-3 Copyright  Oracle Corporation, 1999. All rights reserved. Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution How is it raised? – An Oracle error occurs. – You raise it explicitly. How do you handle it? – Trap it with a handler. – Propagate it to the calling environment. What is an exception? Identifier in PL/SQL that is raised during execution How is it raised? – An Oracle error occurs. – You raise it explicitly. How do you handle it? – Trap it with a handler. – Propagate it to the calling environment.

4 23-4 Copyright  Oracle Corporation, 1999. All rights reserved. Handling Exceptions Trap the exception DECLARE BEGIN END; Exception is raised EXCEPTION Exception is trapped Propagate the exception DECLARE BEGIN END; Exception is raised EXCEPTION Exception is not trapped Exception propagates to calling environment

5 23-5 Copyright  Oracle Corporation, 1999. All rights reserved. Exception Types Predefined Oracle Server Non-predefined Oracle Server User-defined Predefined Oracle Server Non-predefined Oracle Server User-defined} Implicitly raised Explicitly raised

6 23-6 Copyright  Oracle Corporation, 1999. All rights reserved. Trapping Exceptions EXCEPTION WHEN exception1 [OR exception2...] THEN statement1; statement2;... [WHEN exception3 [OR exception4...] THEN statement1; statement2;...] [WHEN OTHERS THEN statement1; statement2;...] EXCEPTION WHEN exception1 [OR exception2...] THEN statement1; statement2;... [WHEN exception3 [OR exception4...] THEN statement1; statement2;...] [WHEN OTHERS THEN statement1; statement2;...] SyntaxSyntax

7 23-7 Copyright  Oracle Corporation, 1999. All rights reserved. Trapping Exceptions Guidelines WHEN OTHERS is the last clause. EXCEPTION keyword starts exception- handling section. Several exception handlers are allowed. Only one handler is processed before leaving the block. WHEN OTHERS is the last clause. EXCEPTION keyword starts exception- handling section. Several exception handlers are allowed. Only one handler is processed before leaving the block.

8 23-8 Copyright  Oracle Corporation, 1999. All rights reserved. Trapping Predefined Oracle Server Errors Reference the standard name in the exception-handling routine. Sample predefined exceptions: – NO_DATA_FOUND – TOO_MANY_ROWS – INVALID_CURSOR – ZERO_DIVIDE – DUP_VAL_ON_INDEX Reference the standard name in the exception-handling routine. Sample predefined exceptions: – NO_DATA_FOUND – TOO_MANY_ROWS – INVALID_CURSOR – ZERO_DIVIDE – DUP_VAL_ON_INDEX

9 23-9 Copyright  Oracle Corporation, 1999. All rights reserved.

10 23-10 Copyright  Oracle Corporation, 1999. All rights reserved. Predefined Exception BEGIN EXCEPTION WHEN NO_DATA_FOUND THEN statement1; statement2; WHEN TOO_MANY_ROWS THEN statement1; WHEN OTHERS THEN statement1; statement2; statement3; END; SyntaxSyntax

11 23-11 Copyright  Oracle Corporation, 1999. All rights reserved. Trapping Non-Predefined Oracle Server Errors Declare Name the exception Name the exception Associate Code the PRAGMA EXCEPTION_INIT Code the PRAGMA EXCEPTION_INIT Declarative section Reference Handle the raised exception Handle the raised exception Exception-handlingsection

12 23-12 Copyright  Oracle Corporation, 1999. All rights reserved. DECLARE e_emps_remainingEXCEPTION; PRAGMA EXCEPTION_INIT ( e_emps_remaining, -2292); v_deptno dept.deptno%TYPE := &p_deptno; BEGIN DELETE FROM dept WHERE deptno = v_deptno; COMMIT; EXCEPTION WHEN e_emps_remaining THEN DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' || TO_CHAR(v_deptno) || '. Employees exist. '); END; DECLARE e_emps_remainingEXCEPTION; PRAGMA EXCEPTION_INIT ( e_emps_remaining, -2292); v_deptno dept.deptno%TYPE := &p_deptno; BEGIN DELETE FROM dept WHERE deptno = v_deptno; COMMIT; EXCEPTION WHEN e_emps_remaining THEN DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' || TO_CHAR(v_deptno) || '. Employees exist. '); END; Non-Predefined Error Trap for Oracle Server error number 2292, an integrity constraint violation. Trap for Oracle Server error number –2292, an integrity constraint violation. e_emps_remaining EXCEPTION; 1 PRAGMA EXCEPTION_INIT ( e_emps_remaining, -2292); 2 e_emps_remaining 3

13 23-13 Copyright  Oracle Corporation, 1999. All rights reserved. Functions for Trapping Exceptions SQLCODE Returns the numeric value for the error code SQLERRM Returns the message associated with the error number SQLCODE Returns the numeric value for the error code SQLERRM Returns the message associated with the error number

14 23-14 Copyright  Oracle Corporation, 1999. All rights reserved. Functions for Trapping Exceptions DECLARE v_error_code NUMBER; v_error_message VARCHAR2(255); BEGIN... EXCEPTION... WHEN OTHERS THEN ROLLBACK; v_error_code := SQLCODE ; v_error_message := SQLERRM ; INSERT INTO errors VALUES(v_error_code, v_error_message); END; ExampleExample SQLCODE SQLERRM

15 23-15 Copyright  Oracle Corporation, 1999. All rights reserved. Trapping User-Defined Exceptions Name the exception Name the exception Declare Declarativesection Raise Explicitly raise the exception by using the RAISE statement Explicitly raise the exception by using the RAISE statement Executablesection Reference Handle the raised exception Handle the raised exception Exception-handlingsection

16 23-16 Copyright  Oracle Corporation, 1999. All rights reserved. User-Defined Exception DECLARE e_invalid_product EXCEPTION; BEGIN UPDATEproduct SETdescrip = '&product_description' WHEREprodid = &product_number; IF SQL%NOTFOUND THEN RAISE e_invalid_product; END IF; COMMIT; EXCEPTION WHEN e_invalid_product THEN DBMS_OUTPUT.PUT_LINE('Invalid product number.'); END; ExampleExample e_invalid_product EXCEPTION; 1 RAISE e_invalid_product; 2 e_invalid_product 3

17 23-17 Copyright  Oracle Corporation, 1999. All rights reserved. Calling Environments SQL*Plus Procedure Builder Oracle Developer Forms Precompiler application An enclosing PL/SQL block Displays error number and message to screen Accesses error number and message in a trigger by means of the ERROR_CODE and ERROR_TEXT packaged functions Accesses exception number through the SQLCA data structure Traps exception in exception- handling routine of enclosing block

18 23-18 Copyright  Oracle Corporation, 1999. All rights reserved. Propagating Exceptions BEGIN SELECT... UPDATE... IF SQL%NOTFOUND THEN RAISE e_no_rows; END IF; EXCEPTION WHEN e_integrity THEN... WHEN e_no_rows THEN... END; DECLARE... e_no_rowsexception; e_integrityexception; PRAGMA EXCEPTION_INIT (e_integrity, -2292); BEGIN FOR c_record IN emp_cursor LOOP END LOOP; EXCEPTION WHEN NO_DATA_FOUND THEN... WHEN TOO_MANY_ROWS THEN... END; Subblocks can handle an exception or pass the exception to the enclosing block. BEGIN SELECT... UPDATE... IF SQL%NOTFOUND THEN RAISE e_no_rows; END IF; EXCEPTION WHEN e_integrity THEN... WHEN e_no_rows THEN... END;

19 23-19 Copyright  Oracle Corporation, 1999. All rights reserved. RAISE_APPLICATION_ERROR Procedure Syntax A procedure that lets you issue user- defined error messages from stored subprograms Called only from an executing stored subprogramSyntax A procedure that lets you issue user- defined error messages from stored subprograms Called only from an executing stored subprogram raise_application_error (error_number, message[, {TRUE | FALSE}]);

20 23-20 Copyright  Oracle Corporation, 1999. All rights reserved. RAISE_APPLICATION_ERROR Procedure Used in two different places: – Executable section – Exception section Returns error conditions to the user in a manner consistent with other Oracle Server errors Used in two different places: – Executable section – Exception section Returns error conditions to the user in a manner consistent with other Oracle Server errors

21 23-21 Copyright  Oracle Corporation, 1999. All rights reserved. Summary Exception types: – Predefined Oracle Server error – Non-predefined Oracle Server error – User-defined error Exception trapping Exception handling: – Trap the exception within the PL/SQL block. – Propagate the exception. Exception types: – Predefined Oracle Server error – Non-predefined Oracle Server error – User-defined error Exception trapping Exception handling: – Trap the exception within the PL/SQL block. – Propagate the exception.

22 23-22 Copyright  Oracle Corporation, 1999. All rights reserved. Practice Overview Handling named exceptions Creating and invoking user-defined exceptions Handling named exceptions Creating and invoking user-defined exceptions

23 23-23 Copyright  Oracle Corporation, 1999. All rights reserved.

24 23-24 Copyright  Oracle Corporation, 1999. All rights reserved.


Download ppt "Copyright  Oracle Corporation, 1999. All rights reserved. 23 Handling Exceptions."

Similar presentations


Ads by Google