Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.

Similar presentations


Presentation on theme: "1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an."— Presentation transcript:

1 1 Handling Exceptions Part F

2 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an exception? Identifier in PL/SQL that is raised during execution How is it raised? How is it raised? An Oracle error occurs. An Oracle error occurs. For example, if the error ORA-01403 occurs when no rows are retrieved from the database in a SELECT statement, then PL/SQL raises the exception NO_DATA_FOUND. For example, if the error ORA-01403 occurs when no rows are retrieved from the database in a SELECT statement, then PL/SQL raises the exception NO_DATA_FOUND. You raise it explicitly. You raise it explicitly. The exception being raised may be either user defined or predefined. The exception being raised may be either user defined or predefined. How do you handle it? How do you handle it? Trap it with a handler. Trap it with a handler. What is an exception? Identifier in PL/SQL that is raised during execution What is an exception? Identifier in PL/SQL that is raised during execution How is it raised? How is it raised? An Oracle error occurs. An Oracle error occurs. For example, if the error ORA-01403 occurs when no rows are retrieved from the database in a SELECT statement, then PL/SQL raises the exception NO_DATA_FOUND. For example, if the error ORA-01403 occurs when no rows are retrieved from the database in a SELECT statement, then PL/SQL raises the exception NO_DATA_FOUND. You raise it explicitly. You raise it explicitly. The exception being raised may be either user defined or predefined. The exception being raised may be either user defined or predefined. How do you handle it? How do you handle it? Trap it with a handler. Trap it with a handler.

3 3 Handling Exceptions Trap the exception Trap the exception If the exception is raised in the executable section of the block and there is no corresponding exception handler, the PL/SQL block terminates with failure and the exception is propagated to the calling environment. DECLARE BEGIN END; Exception is raised EXCEPTION Exception is trapped

4 4 Exception Types Predefined Oracle Server Predefined Oracle Server Non-predefined Oracle Server Non-predefined Oracle Server User-defined User-defined Predefined Oracle Server Predefined Oracle Server Non-predefined Oracle Server Non-predefined Oracle Server User-defined User-defined} Implicitly raised Explicitly raised

5 5 Predefined Exceptions Most common errors that occur in programs Most common errors that occur in programs PL/SQL language: PL/SQL language: Assigns exception name Assigns exception name Provides built-in exception handler for each predefined exception Provides built-in exception handler for each predefined exception System automatically displays error message informing user of nature of problem System automatically displays error message informing user of nature of problem Can create exception handlers to display alternate error messages Can create exception handlers to display alternate error messages

6 6 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;...] Syntax Syntax

7 7 Trapping Exceptions Guidelines WHEN OTHERS is the last clause. WHEN OTHERS is the last clause. EXCEPTION keyword starts exception- handling section. EXCEPTION keyword starts exception- handling section. Several exception handlers are allowed. Several exception handlers are allowed. Only one handler is processed before leaving the block. Only one handler is processed before leaving the block. You can have at most one OTHERS clause. You can have at most one OTHERS clause. Exceptions cannot appear in assignment statements or SQL statements. Exceptions cannot appear in assignment statements or SQL statements. WHEN OTHERS is the last clause. WHEN OTHERS is the last clause. EXCEPTION keyword starts exception- handling section. EXCEPTION keyword starts exception- handling section. Several exception handlers are allowed. Several exception handlers are allowed. Only one handler is processed before leaving the block. Only one handler is processed before leaving the block. You can have at most one OTHERS clause. You can have at most one OTHERS clause. Exceptions cannot appear in assignment statements or SQL statements. Exceptions cannot appear in assignment statements or SQL statements.

8 8 Trapping Predefined Oracle Server Errors  Common errors that have been given predefined names Reference the standard name in the exception- handling routine. Reference the standard name in the exception- handling routine. Sample predefined exceptions: Sample predefined exceptions:  Common errors that have been given predefined names Reference the standard name in the exception- handling routine. Reference the standard name in the exception- handling routine. Sample predefined exceptions: Sample predefined exceptions:

9 9 Predefined Exception BEGIN EXCEPTION WHEN NO_DATA_FOUND THEN statement1; statement2; WHEN TOO_MANY_ROWS THEN statement1; WHEN OTHERS THEN statement1; statement2; statement3; END; Syntax Syntax

10 10 Undefined Exceptions Less common errors Less common errors Do not have predefined names Do not have predefined names Must explicitly declare exception in program’s declaration section Must explicitly declare exception in program’s declaration section Associate new exception with specific Oracle error code Associate new exception with specific Oracle error code Create exception handler in exception section Create exception handler in exception section Using same syntax as for predefined exceptions Using same syntax as for predefined exceptions

11 11 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 Less-common errors that have not been given predefined names

12 12 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 13 Functions for Trapping Exceptions SQLCODE Returns the numeric value for the error code SQLCODE Returns the numeric value for the error code SQLERRM Returns the message associated with the error number SQLERRM Returns the message associated with the error number SQLCODE Returns the numeric value for the error code SQLCODE Returns the numeric value for the error code SQLERRM Returns the message associated with the error number SQLERRM Returns the message associated with the error number

14 14 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; Example Example SQLCODE SQLERRM

15 15 User-defined Exceptions Do not raise Oracle runtime error Do not raise Oracle runtime error Require exception handling to Require exception handling to Enforce business rules Enforce business rules Ensure integrity of database Ensure integrity of database

16 16 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

17 17 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

18 18 General Syntax for Declaring, Raising, and Handling a User- defined Exception


Download ppt "1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an."

Similar presentations


Ads by Google