1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Error handling pptx

28 201 0
Tài liệu được quét OCR, nội dung có thể không chính xác
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Error handling
Thể loại PowerPoint presentation
Định dạng
Số trang 28
Dung lượng 1,17 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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.

Trang 1

Error Handling

Trang 2

- ., ,,., ,, ,,,.,.,., , Á -— —— ŸŸ

Objectives

° Identify common exceptions

* Describe the three basic types of exceptions

* Write exception handling routines

25-2 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder

Trang 3

Objectives

Coe Meee e eee eee eee H EEE HREM EO EDEL OO EEE DERE EEO DERE E DEES EDESS DEH EEOEEEEE EES EO SESE EESESED SHEET EH HEHE SESE HHO ES ESEEOHH EDS ED EEO ES

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

e Identify common exceptions

e Describe the three basic types of exceptions

e Write exception handling routines

Trang 4

- An Oracle error occurs

~ You raise it explicitly

» How do you handle it?

—- Trap it with a handler

- Propagate It to the calling environment

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder

Trang 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

se 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

e 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

BORER Dewees e 6 8 B6 6M 689 6 6096 06 8i 6E 8 6 6 8:6 6,6 81 84.46.4614 94-0 8 6 6 66.0440 9/6 0 68.9 884406 6 0 62464 4000 ko ø 9 4 8 Ác H 9 6 B4 6.0 46 406 0790 6 6 0 0 0 9 0 6 0 9 6 8 6Á 4 0460 6 6 E GÀ B ho Đ B6 giảng bạ 6 Bế

Trang 6

ORACLE’

" Œ a.a DD - SEES EEE

Exception Types

¢ Predefined Oracle7 Server implicitly

* Non-predefined Oracle7 Server Raised

25-6 introduction to Oracle: SQL and PL/SQL Using Procedure Builder

Trang 7

Predefined Oracle7 | One of approximately 20 | Do not declare, and allow the Server error most errors that occur Oracle7 Server to raise them

often in PL/SQL code implicitly

Non-Predefined Any other standard Declare within the declarative

Oracle7 Server Oracle7 Server error section, and allow the Oracle7

User-defined error | A condition that the Declare within the declarative

developer determines is section, and raise explicitly

abnormal

oe a ating seaveeesensenssescenecssesssssaseseveseesseesusssessessssssuadusdpessoassnessspopauesssstastocseccsesscesececessecesce

Trang 8

Trapping Exceptions: Guidelines

¢ WHEN OTHERS is the fast clause

¢ EXCEPTION keyword starts exception-handling section

* Several exception handlers allowed

¢ Only one handler is processed before leaving the

block

25-8 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder

Trang 9

Syntax

EXCEPTION WHEN exception! [OR exception2

statement] ; Statement2;

[WHEN exception3 [OR exception4

statement] ; statement2;

¬ [WHEN OTHERS THEN statement! ;

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

e Place the WHEN OTHERS clause after all other exception handling clauses

e You can have at most one WHEN OTHERS clause

e Begin exception-handling section of the block with the keyword EXCEPTION

e Define several exception handlers, each with their own set of actions, for the

block

e 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

Error Handling

Trang 10

ORACLE’

Ÿ ee eee reer ee re eee eee ee ee ee eee eee ee eee eee Eee es

Trapping Predefined Oracle7 Server Errors

5 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

25.10 Introduction to Oracle: SOL and PL/SQL Using Procedure Builder

Trang 11

Trapping Predefined Oracle7 Server Exceptions

“xố vẽ .( (ái ooaaaaodadoanannndioddnananaa-a Trap a predefined Oracle7 Server error by referencing its standard name within the Corresponding exception-handling routine

Sample Predefined Exceptions

Oracle Server Exception Name Error Number | Description

than one row

INVALID_CURSOR ORA-01001 Tegal 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

WPA You can use the debugging capabilities in Procedure Builder to identify and trap

0 exceptions prior to moving procedures to the database

cA For more information, see

iB PL/SQL User's Guide and Reference, Release 2.3 “Predefined Exceptions” section

Trang 12

BEGIN

SELECT id

INTO v_id FROM 8 product

TEXT_I0.PUT_LINE(TO_CHAR(v_product_id)) | |

‘_is invalid.’);

ROLLBACK;

TEXT_1I0.PUT_LINE (‘Data corruption in S PRODUCT.’);

WHEN OTHERS THEN

ROLLBACK;

TEXT_IO.PUT LINE (‘Other error occurred.’);

END elim_ inventory;

Trang 13

DELETE FROM s inventory

TEXT IQ.PUT_LINE(’Data corruption in S PRODUCT.’);

WHEN OTHERS THEN

~-

Trang 14

Oracle7 Server Errors

Declare -———-jei Associate + Reference

Declarative Section Exception-handiing

Non-Predefined Error: Example Trap for Oracle7 Server error number -2292 an integrity constraint violation

[DECLARE]

e products remaining EXCEPTION;

PRAGMA EXCEPTION INIT (

e_products_remaining, ~2292);

BEGIN

EXCEPTION WHEN e products remaining THEN © TEYT_IO.PUT_LINE (‘Referential integrity

eee eee eee eee eee eee eee ee eee eee eee eee ee ee eee ee eee ee eee eee ee eee ee eee eee eee eee ee eee eee eee eee SECC CeCe SS TST SSOSTCCCeTOCOSCO TOSS Cee ee eS eS

25-14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder

Trang 15

«HAI ÓC 3 9 ÓC go ĐÓ o0 Bo do HUẾ oi 0i 0đ in HP CÔ 0Ó 0 R9 9 60 080 k0 9 02066 0.46 6.0 80 00.0 106 00000 60 0300 608.9 6 066 0 009096006 6089060800669 044 969 b6 0 0 6.8 nề 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

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

IN For more information, see

2 Oracle7 Server Messages, Release 7.3

Error Handling

Trang 16

ORACLE’

` ẶÈằÈ©¬ằạan , , - anŨ OE

Trapping User-Defined Exceptions

Declare a Raise > Reference

Declarative Executable Exception-handling Section Section Section

se Name the « Explicitly raise the * Handle the exception exception by using raised

the RAISE statement exception

User-Defined Exception: Example

If there is product in stock, halt processing, and print a

message to the user

WHEN e_amount_ remaining THEN ©

TEXT_IO.PUT_LINE (‘There is still an amount

in stock.’);

END;

@ Name the exception © Explicitly raise the © Handle the raised

exception by using the exception

RAISE statement

OMe rem remeron reer eee tee seer ee ee eens Erase Ree etna see esr E tesa mana tH eee DEES OOP OE HEHE EEE E EEE E EH EEH EH OREM Deere MDE e EEE ee ne eee HEreees

25-16 Introduction to Oracle: SQL and PL/SQL Using Procedure Bullder

Trang 17

tuỒŨẮ đ r Ắ.Ắ Ắ Ố.Ó Ắ - ._ ŠÖ 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

where: exception is the name of the exception

2 Raise the exception explicitly within the executable section using the RAISE

where: exception is the previously declared exception

3 Reference the declared exception within the corresponding exception handling routine

KĨ R9 90 0,48 009 06499 6 866489 606.80 9 6.89484000889806 4.808.096 868.0 68.6 0 2.619 9 8 41604 0: 8.604 5 8 4 640 6 k4 64604 ener b ane na ss eenb een eeeteeseeatacanssoane

Trang 18

ORACLE’

Ree ee emcee meee eee EEO EERO REECE OEE O HEHEHE HEHE SEES ORE EOE EEE EOREDOREEH HEE SEH SHOR ESS SOSHE AES EHESEHE DES ESHER ERED ENOS

Functions for Trapping Exceptions

¢ WHEN OTHERS exception handler

- Traps all exceptions not yet handled

— Is the last handler

Functions for Trapping Exceptions: Example

Store the error code and error message for any

WHEN OTHERS THEN

Trang 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 ail 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

SQLERRM Returns character data containing the message associated with

the error number

Example SQLCODE Values

Trang 20

| Developer/2000 Accesses error number and message

Forms ina trigger

Precompiler Accesses exception number through

Application SQLCA

An Enclosing Traps exception in exception

PL/SQL Block handling routine of enclosing block

Sub blocks can handle PRAGMA EXCEPTION INIT (

an exception or pass the e_integrity, -2292);

exception to the FOR ¢,record IN emp, cursor LOOP

UPDATE

IP SQLtMWOTPOUND THEN RAISE e no rows;

: EMD IF;

> WHEN e integrity THEN ;

WHEN e_no_rows THEN

¬ ẮÂỐẮẤỐỐỐ ốỐ

EXCEPTION WHEN NO_DATA_FOUND THEN

WHEN TOO_MANY_ROWS THEN

Trang 21

Propagating Excepthoms —¬—¬— em -Ặ—=nkseseereree

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

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

Trang 22

- Predefined Oracle7 Server error

— Non-Predefined Oracle? Server error

ORR eee mee ee mere neta re eH atte Ee EOE TEER 0 0Á nề R g4 0 024 0 09 4 0006 4 4 6 8 06 Bi Hộ mo g6 0 6.Á 000B 4 6 2 0 0 49 0 00 8 ác 4 8 0 4 6 0 0 H DEO EHSE ESO ORDO DE EE CEE ESHER DESO MDE eh ee Hee neeeee

25-22 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder

Ngày đăng: 24/01/2014, 10:20

TỪ KHÓA LIÊN QUAN

w