4exception Handling
4exception Handling
What is an Exception ?
-2 Document Name
CONFIDENTIAL
Handling Exception
DECLARE DECLARE
BEGIN BEGIN
Exception Exception
is raised is raised
EXCEPTION EXCEPTION
Exception Exception is
is trapped END; END; not trapped
Exception
propagates to calling
environment
-3 Document Name
CONFIDENTIAL
Exception Types
Trapping Exceptions
Syntax
EXCEPTION
WHEN exception1 [OR exception2 . . .] THEN
statement1;
statement2;
. . .
[WHEN exception3 [OR exception4 . . .] THEN
statement1;
statement2;
. . .]
[WHEN OTHERS THEN
statement1;
statement2;
. . .]
-4 Document Name
CONFIDENTIAL
Trapping Predefined 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
Example:
BEGIN
SELECT empno,ename,sal INTO v_empno,v_ename,v_sal FROM emp
WHERE empno =&empno;
INSERT INTO emp_his VALUES(v_empno,v_ename,v_sal);
DELETE FROM emp WHERE empno=v_empno;
EXCEPTION
WHEN TOO_MANY_ROWS OR NO_DATA_FOUND THEN
ROLLBACK;
INSERT INTO temp VALUES (’Empno not found, or more than one emp’);
COMMIT;
WHEN OTHERS THEN
ROLLBACK;
END;
-5 Document Name
CONFIDENTIAL
Trapping User-Defined Exceptions
-6 Document Name
CONFIDENTIAL
User-Defined Exception
Example
DECLARE
e_invalid_product EXCEPTION; 1
BEGIN
UPDATE product
SET descrip = '&product_description'
WHERE prodid = &product_number;
IF SQL%NOTFOUND THEN
RAISE e_invalid_product; 2
END IF;
COMMIT;
EXCEPTION
WHEN e_invalid_product THEN 3
DBMS_OUTPUT.PUT_LINE('Invalid product number.');
END;
-7 Document Name
CONFIDENTIAL
Exception Propagation
Step #1:
The current block is searched for a handler. If not found, go to step 2.
Step# 2 :
If an enclosing block is found, it is searched for it handler.
Step# 3:
Step1 & Step 2 are repeated until either there are no more enclosing
blocks, or a handler is found .
• If there are no more enclosing blocks, the exception is passed back to the
calling environment (SQL *Plus, SQL *Forms)
• If the handler is found ,it is executed .when done the block in which the
handler was found is terminated, and control is passed to thee enclosing
block
Quick notes
• Only one handler per block may be active at a time
• If an exception is raised in a handler, the search for a handler for the new
exception begins in the enclosing block of the current block
-8 Document Name
CONFIDENTIAL
Exception Propagation
• Example
DECLARE
. . .
e_no_rows exception;
BEGIN
FOR c_record IN emp_cursor LOOP
BEGIN
Sub blocks can handle an SELECT ...
exception or pass the UPDATE ...
IF SQL%NOTFOUND THEN
exception to the enclosing RAISE e_no_rows;
block. END IF;
EXCEPTION
WHEN e_no_rows THEN ...
END;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN . . .
WHEN TOO_MANY_ROWS THEN . . .
END;
-9 Document Name
CONFIDENTIAL
Error Reporting Functions
SQLCODE
Returns the numeric value for the error code
SQLERRM
Returns the message associated with the error number
• Example
DECLARE
v_error_code NUMBER;
v_error_message VARCHAR2(255);
BEGIN
...
EXCEPTION
...
WHEN OTHERS THEN
ROLLBACK;
v_error_code := SQLCODE;
SQLCODE
v_error_message := SQLERRM
SQLERRM ;
INSERT INTO errors VALUES(v_error_code,
v_error_message);
END;
-
10 Document Name
CONFIDENTIAL
RAISE_APPLICATION_ERROR Procedure
• Syntax:
raise_application_error (error_number,
message[, {TRUE | FALSE}]);
• Example:
BEGIN
IF emp_id <=0 THEN
raise_application_error (-20100,’Employee number must be> 0’);
ELSE
DELETE
FROM emp
WHERE EMPNO =EMPID;
END IF;
END;
- 11 Document Name
CONFIDENTIAL
Thank You