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

Tài liệu Error handling ppt

28 304 0
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
Trường học Oracle University
Chuyên ngành Database Management
Thể loại Bài giảng
Định dạng
Số trang 28
Dung lượng 191 KB

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

Nội dung

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć2... Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć4... A block will always terminate when PL/SQLrai

Trang 1

Error Handling

25

Trang 2

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

Trang 3

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

Trang 4

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć4

Trang 5

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/SQLraises 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 Forexample, if the error ORA-01403 occurs when no rows are retrieved from thedatabase, then PL/SQL raises the exception NO_DATA_FOUND

D You raise an exception explicitly by issuing the RAISE statement within theblock 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 IfPL/SQL successfully handles the exception, then the exception does not propagate tothe enclosing block or environment

Propagating an Exception

The other method for handling an exception is to allow it to propagate to the callingenvironment If the exception is raised in the executable section of the block andthere is no corresponding exception handler, the PL/SQL block terminates withfailure

Trang 6

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć6

Trang 7

Do not declare, and allow theOracle7 Server to raise themimplicitly.

developer determines isabnormal

Declare within the declarative

section, and raise explicitly.

Trang 8

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

Trang 9

where: exception is the standard name of a predefined exception

or the name of a user-defined exceptiondeclared 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 theblock

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

Trang 10

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć10

Trang 11

Trapping Predefined Oracle7 Server Exceptions

Trap a predefined Oracle7 Server error by referencing its standard name within thecorresponding 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 trapexceptions prior to moving procedures to the database

For more information, see

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

Trang 12

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć12

Trang 13

Trapping Predefined Oracle7 Server Exceptions continued

DELETE FROM s_inventory

WHERE product_id = v_product_id;

Trang 14

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

1 2 3

Name the exception.

EXCEPTION_INIT.

exception.

3

Trang 15

Trapping NonĆPredefined Oracle7 Server Exceptions

Trap a non-predefined Oracle7 Server error by declaring it first, rather than by usingthe WHEN OTHERS handler The declared exception will be raised implicitly Besure 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 numberusing the pragma EXCEPTION_INIT statement

Syntax

PRAGMA EXCEPTION_INIT (exception, error_number);

where: exception is the previously-declared exception

3. Reference the declared exception within the corresponding exception handlingroutine

For more information, see

Oracle7 Server Messages, Release 7.3.

Trang 16

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć16

1

3 2

1 2 3

Name the exception.

1 Explicitly raise the

exception by using the RAISE statement.

exception.

3

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

Syntax

RAISE exception;

where: exception is the previously declared exception

3. Reference the declared exception within the corresponding exception handlingroutine

Trang 18

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć18

Trang 19

Error Trapping Functions

When an exception is trapped in the WHEN OTHERS section, you can use a set ofgeneric functions for identifying those errors

WHEN OTHERS Exception Handler

The exception-handling section only traps those exceptions specified; any otherexceptions would not be trapped unless you use the WHEN OTHERS exceptionhandler This will trap any exception not yet handled For this reason, the WHENOTHERS 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 theapplication 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 candecide 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

Truncate the value of SQLERRM to a known length before attempting to write it to avariable

Trang 20

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć20

Trang 21

Propagating Exceptions

Instead of trapping an exception within the PL/SQL block, propagate the exception toallow the calling environment to handle it Each calling environment will have itsown 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 theERROR_CODE and ERROR_TEXT packagedfunctions

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 blocksuntil it finds a handler If none of these blocks handle the exception, this causes anunhandled exception in the host environment

When the exception propagates to an enclosing block, the remaining executableactions in that block are bypassed

One advantage of this behavior is that you can enclose statements that require theirown exclusive error handling in their own block, while leaving more general

exception handling to the enclosing block

Trang 22

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

Trang 23

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 anenclosing block

D Terminate the PL/SQL processing with failure by passing the unhandled

exception to the calling environment

Trang 24

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć24

Trang 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

Trang 26

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć26

Trang 27

Practice 25

1. Modify the procedure to handle exceptions The procedure tries to update regionnumbers for existing departments

a. Load the LABS\p25q1a.pls script file Note: The procedure UPDATE_DEPT2

is loaded and compiled

b. Execute the procedure by entering the following text

Trang 28

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 25Ć28

If you have time, complete the following exercise

2. Write a procedure named SALARY_RANGE that prints the names of the

employees that make plus or minus $100 of the salary value entered

a. If there is no employee within that salary range, then print a message to theuser indicating that is the case Use an exception for this case

b. If there are more than 3 employees within that range, then the message shouldindicate how many employees have that salary range

Your results should look like the list below Results have been formatted to fitthe page

Ngày đăng: 17/01/2014, 09:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w