0% found this document useful (0 votes)
12 views12 pages

4exception Handling

Uploaded by

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

4exception Handling

Uploaded by

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

Exception Handling

What is an Exception ?

• In PL/SQL, an error condition is called an exception

• Exceptions can be internally defined (by the runtime system) or user


defined.

• When the program encounters an exception, the normal execution of the


program is disrupted and if the exception is not handled in the code then the
control returns to the host environment.

• This can be prevented by using exception handling and error checking


techniques.

• When an exception is encountered, normal execution stops and control


transfers to the exception-handling part of the PL/SQL block or subprogram.

-2 Document Name
CONFIDENTIAL
Handling Exception

Trap the Exception Propagate the 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

 Oracle predefined Exception


 User-defined Exception

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

Declare Raise Reference

Declarative Executable Exception-handling


section section section

Name the Explicitly raise the Handle the


exception exception by using raised
the RAISE exception
statement

-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

• A procedure that issues user-defined error messages from stored


subprograms
• Error number range is -20000 to -20999
• Range always reserved for user defined errors

• 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

You might also like