Tải bản đầy đủ (.pdf) (28 trang)

Tài liệu Error handling ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (191 KB, 28 trang )

Error Handling
25
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć2
Error Handling 25Ć3
Objectives
When you execute PL/SQL code, you may encounter errors. Errors cause the
PL/SQL block to halt with an exception. You can trap the exception and
perform actions conditionally using exception handlers.
At the end of this lesson, you should be able to
D Identify common exceptions.
D Describe the three basic types of exceptions.
D Write exception handling routines.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć4
Error Handling 25Ć5
Overview
An exception is an identifier in PL/SQL, raised during the execution of a block that
terminates its main body of actions. A block will always terminate when PL/SQL
raises an exception, but you specify an exception handler to perform final actions.
Two Methods for Raising an Exception
D An Oracle error occurs and the associated exception is raised automatically. For
example, if the error ORA-01403 occurs when no rows are retrieved from the
database, then PL/SQL raises the exception NO_DATA_FOUND.
D You raise an exception explicitly by issuing the RAISE statement within the
block. The exception being raised may be either user-defined or predefined.
Trapping an Exception
If the exception is raised in the executable section of the block, processing branches
to the corresponding exception handler in the exception section of the block. If
PL/SQL successfully handles the exception, then the exception does not propagate to
the enclosing block or environment.
Propagating an Exception
The other method for handling an exception is to allow it to propagate to the calling


environment. 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.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć6
Error Handling 25Ć7
Exception Types
You can program for exceptions to avoid disruption at runtime. There are three types
of exceptions.
Exception
Description Directions for Handling
Predefined Oracle7
Server error
One of approximately 20
most errors that occur
often in PL/SQL code.
Do not declare, and allow the
Oracle7 Server to raise them
implicitly.
Non-Predefined
Oracle7 Server
error
Any other standard
Oracle7 Server error.
Declare within the declarative
section, and allow the Oracle7
Server to raise them implicitly.
User-defined error A condition that the
developer determines is
abnormal.
Declare within the declarative

section, and raise explicitly.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć8
Error Handling 25Ć9
Trapping Exceptions
Trap any error by including a corresponding routine within the exception handling
section of the PL/SQL block.
Syntax
EXCEPTION
WHEN exception1 [OR exception2 . . .] THEN
statement1;
statement2;
. . .
[WHEN exception3 [OR exception4 . . . THEN
statement1;
statement2;
. . .]
[WHEN OTHERS THEN
statement1;
statement2;
. . .]
where: exception is the standard name of a predefined exception
or the name of a user-defined exception
declared within the declarative section.
WHEN OTHERS indicates the exception handling routine for any
exception is not listed explicitly.
Guidelines
D Place the WHEN OTHERS clause after all other exception handling clauses.
D You can have at most one WHEN OTHERS clause.
D Begin exception-handling section of the block with the keyword EXCEPTION.
D Define several exception handlers, each with their own set of actions, for the

block.
D When an exception occurs, PL/SQL will process only one handler before leaving
the block.
For more information, see
Oracle Course Catalog to attend Develop Applications with Database Procedures
course.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć10
Error Handling 25Ć11
Trapping Predefined Oracle7 Server Exceptions
Trap a predefined Oracle7 Server error by referencing its standard name within the
corresponding exception-handling routine.
Sample Predefined Exceptions
Exception Name
Oracle Server
Error Number
Description
NO_DATA_FOUND ORA-01403 Single row SELECT returned no data.
TOO_MANY_ROWS ORA-01422 Single row SELECT returned more
than one row.
INVALID_CURSOR ORA-01001 Illegal cursor operation occurred.
ZERO_DIVIDE ORA-01476 Attempted to divide by zero.
DUP_VAL_ON_INDEX ORA-00001 Attempted to insert a duplicate value
into a column that has a unique index.
You can use the debugging capabilities in Procedure Builder to identify and trap
exceptions prior to moving procedures to the database.
For more information, see
PL/SQL User’s Guide and Reference, Release 2.3 “Predefined Exceptions” section.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć12
Error Handling 25Ć13
Trapping Predefined Oracle7 Server Exceptions continued

Example
Eliminate all inventory items for a particular product. Be sure the product number is
valid.
PROCEDURE elim_inventory
(v_product_id IN s_product.id%TYPE) IS
v_id s_product.id%TYPE;
BEGIN
SELECT id
INTO v_id
FROM s_product
WHERE id = v_product_id;
DELETE FROM s_inventory
WHERE product_id = v_product_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
ROLLBACK;
TEXT_IO.PUT_LINE(TO_CHAR(v_product_id)||
’ is invalid.’);
WHEN TOO_MANY_ROWS THEN
ROLLBACK;
TEXT_IO.PUT_LINE(’Data corruption in S_PRODUCT.’);
WHEN OTHERS THEN
ROLLBACK;
TEXT_IO.PUT_LINE(’Other error occurred.’);
END elim_inventory;
Only one exception is raised and handled at any time.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć14
1
2

3
Name the exception.1 Code the pragma
EXCEPTION_INIT.
2 Handle the raised
exception.
3
Error Handling 25Ć15
Trapping NonĆPredefined Oracle7 Server Exceptions
Trap a non-predefined Oracle7 Server error by declaring it first, rather than by using
the WHEN OTHERS handler. The declared exception will be raised implicitly. Be
sure to reference the declared exception in the exception handling section.
Trapping a NonĆPredefined Oracle7 Server Exception
1. Declare the name for the exception within the declarative section.
Syntax
exception EXCEPTION;
where: exception is the name of the exception.
2. Associate the declared exception with the standard Oracle7 Server error number
using the pragma EXCEPTION_INIT statement.
Syntax
PRAGMA EXCEPTION_INIT (exception, error_number);
where: exception is the previously-declared exception.
error_number is a standard Oracle7 Server error number.
3. Reference the declared exception within the corresponding exception handling
routine.
For more information, see
Oracle7 Server Messages, Release 7.3.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć16
1
3
2

1
2
3
Name the exception.
1 Explicitly raise the
exception by using the
RAISE statement.
2 Handle the raised
exception.
3
Error Handling 25Ć17
Trapping UserĆDefined Exceptions
Trap a user-defined exception by declaring it and raising it explicitly.
Trapping a UserĆDefined Exception
1. Declare the name for the user-defined exception within the declarative section.
Syntax
exception EXCEPTION;
where: exception is the name of the exception.
2. Raise the exception explicitly within the executable section using the RAISE
statement.
Syntax
RAISE exception;
where: exception is the previously declared exception.
3. Reference the declared exception within the corresponding exception handling
routine.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć18
Error Handling 25Ć19
Error Trapping Functions
When an exception is trapped in the WHEN OTHERS section, you can use a set of
generic functions for identifying those errors.

WHEN OTHERS Exception Handler
The exception-handling section only traps those exceptions specified; any other
exceptions would not be trapped unless you use the WHEN OTHERS exception
handler. This will trap any exception not yet handled. For this reason, the WHEN
OTHERS is be the last exception handler defined.
The WHEN OTHERS handler traps all exceptions not already trapped. Some Oracle
tools have their own predefined exceptions that you can raise to cause events in the
application. WHEN OTHERS also traps these exceptions.
Functions for Error Trapping
When an exception occurs, you can identify the associated error code or error
message by using two functions. Based on the values of the code or message, you can
decide what subsequent action to take based upon the error.
Function
Description
SQLCODE Returns the numeric value for the error code. You can assign it
to a NUMBER variable.
SQLERRM Returns character data containing the message associated with
the error number.
Example SQLCODE Values
SQLCODE Value Description
0 No exception encountered.
1 User-defined exception.
+100 NO_DATA_FOUND exception.
negative_number Another Oracle7 Server error number.
Truncate the value of SQLERRM to a known length before attempting to write it to a
variable.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć20
Error Handling 25Ć21
Propagating Exceptions
Instead of trapping an exception within the PL/SQL block, propagate the exception to

allow the calling environment to handle it. Each calling environment will have its
own way of displaying and accessing errors.
Calling Environment Exception Handling
Calling Environment
Exception Handling Capabilities
SQL*Plus Unhandled exception number and message are
displayed on the screen.
Procedure Builder Unhandled exception number and message are
displayed on the screen.
Developer/2000 Forms Unhandled exception number and message are
accessible in a trigger by means of the
ERROR_CODE and ERROR_TEXT packaged
functions.
Precompiler application Unhandled exception number is accessible through
the SQLCA data structure.
An enclosing PL/SQL block Unhandled exceptions can be trapped by the
exception handling routine of the enclosing block.
Propagating an Exception in a SubĆblock
When a sub-block handles an exception, it terminates normally, and control resumes
in the enclosing block immediately after the sub-block END statement.
However, if PL/SQL raises an exception and the current block does not have a
handler for that exception, the exception propagates in successive enclosing blocks
until it finds a handler. If none of these blocks handle the exception, this causes an
unhandled exception in the host environment.
When the exception propagates to an enclosing block, the remaining executable
actions in that block are bypassed.
One advantage of this behavior is that you can enclose statements that require their
own exclusive error handling in their own block, while leaving more general
exception handling to the enclosing block.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć22

Error Handling 25Ć23
Summary
Program for exceptions that may arise during the execution of the PL/SQL block.
Exception Types
D Predefined Oracle7 Server error.
D Non-predefined Oracle7 Server error.
D User-defined error.
Handle Exceptions
D Trap the exception within the PL/SQL block in an exception handling routine.
D Allow the exception to propagate to the calling environment.
Propagate Exceptions
D Propagate an exception through a series of nested blocks.
D Terminate PL/SQL processing with success by handling the exception in an
enclosing block.
D Terminate the PL/SQL processing with failure by passing the unhandled
exception to the calling environment.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć24
Error Handling 25Ć25
Practice Overview
In this practice, you create exception handlers for specific situations.
Practice Contents
D Handling named exceptions
D Creating and invoking user-defined exceptions

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×