Sie sind auf Seite 1von 18

Handling Exceptions

Part F

By: Deepak Malusare 1


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.
 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.
 The exception being raised may be either user defined or
predefined.
 How do you handle it?
 Trap it with a handler.
By: Deepak Malusare 2
Handling Exceptions
 Trap the exception  If the exception is raised
DECLARE in the executable section
of the block and there is
BEGIN no corresponding
Exception
is raised exception handler, the
EXCEPTION
PL/SQL block
Exception
is trapped END; terminates with failure
and the exception is
propagated to the calling
environment.

By: Deepak Malusare 3


Exception Types

 Predefined Oracle Server


 Non-predefined Oracle Server }
Implicitly
raised
 User-defined Explicitly raised

By: Deepak Malusare 4


Predefined Exceptions
 Most common errors that occur in programs
 PL/SQL language:
 Assigns exception name
 Provides built-in exception handler for each
predefined exception
 System automatically displays error message
informing user of nature of problem
 Can create exception handlers to display
alternate error messages
By: Deepak Malusare 5
Trapping Exceptions
 Syntax
EXCEPTION
WHEN exception1 [OR exception2 . . .] THEN
statement1;
statement2;
. . .
[WHEN exception3 [OR exception4 . . .] THEN
statement1;
statement2;
. . .]
[WHEN OTHERS THEN
statement1;
statement2;
. . .]

By: Deepak Malusare 6


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.
 You can have at most one OTHERS clause.

 Exceptions cannot appear in assignment


statements or SQL statements.
By: Deepak Malusare 7
Trapping Predefined
Oracle Server Errors
 Common errors that have been given predefined
names
 Reference the standard name in the exception-
handling routine.
 Sample predefined exceptions:
Error Code Exception Name Description
ORA-00001 DUP_VAL_ON_INDEX Unique constraint violated
ORA-01001 INVALID_CURSOR Illegal cursor operation
ORA-01403 NO_DATA_FOUND Query returns no records
ORA-01422 TOO_MANY_ROWS Query returns more rows than expected
ORA-01476 ZERO_DIVIDE Division by zero
ORA-01722 INVALID_NUMBER Invalid numeric conversion
ORA-06502 VALUE_ERROR Error in arithmetic or numeric function operation

By: Deepak Malusare 8


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

By: Deepak Malusare 9


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

By: Deepak Malusare 10


Trapping Non-Predefined Oracle
Server Errors
Less-common errors that have not been given
predefined names

Declare Associate Reference

Declarative section Exception-handling


section

• Name the • Code the PRAGMA • Handle the


exception EXCEPTION_INIT raised
exception
By: Deepak Malusare 11
Non-Predefined Error
 Trap for Oracle Server error number
–2292, an integrity constraint violation.
DECLARE
e_emps_remaining EXCEPTION;
e_emps_remaining EXCEPTION; 1
PRAGMA
PRAGMA EXCEPTION_INIT
EXCEPTION_INIT ((
e_emps_remaining,-2292);
e_emps_remaining, -2292); 2
v_deptno dept.deptno%TYPE := &p_deptno;
BEGIN
DELETE FROM dept
WHERE deptno = v_deptno;
COMMIT;
EXCEPTION
WHEN e_emps_remaining THEN 3
DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' ||
TO_CHAR(v_deptno) || '. Employees exist. ');
END;

By: Deepak Malusare 12


Functions for Trapping
Exceptions
 SQLCODE
Returns the numeric value for the error code
 SQLERRM
Returns the message associated with the error
number

By: Deepak Malusare 13


Functions for Trapping
 Example
DECLARE
Exceptions
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;

By: Deepak Malusare 14


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

By: Deepak Malusare 15


Trapping User-Defined
Exceptions

Declare Raise Reference

Declarative Executable Exception-handling


section section section

• Name the • Explicitly raise • Handle the


exception the exception by raised
using the RAISE exception
statement

By: Deepak Malusare 16


User-Defined Exception
Example
DECLARE
EXCEPTION;
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
e_invalid_product THEN 3
DBMS_OUTPUT.PUT_LINE('Invalid product number.');
END;

By: Deepak Malusare 17


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

By: Deepak Malusare 18

Das könnte Ihnen auch gefallen