Tài liệu Error handling ppt

28 303 0
Tài liệu Error handling ppt

Đ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

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 [...]... exclusive error handling in their own block, while leaving more general exception handling to the enclosing block Error Handling 25Ć21 25Ć22 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 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... declared exception within the corresponding exception handling routine Error Handling 25Ć17 25Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 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;... 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... 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... statement Syntax PRAGMA EXCEPTION_INIT (exception, error_ number); where: exception error_ number 3 is the previously-declared exception is a standard Oracle7 Server error number Reference the declared exception within the corresponding exception handling routine For more information, see Oracle7 Server Messages, Release 7.3 Error Handling 25Ć15 1 2 25Ć16 2 Explicitly raise the exception by using the RAISE... TEXT_IO.PUT_LINE(’Other error occurred.’); END elim_inventory; Only one exception is raised and handled at any time Error Handling 25Ć13 1 2 3 1 Name the exception 25Ć14 2 Code the pragma EXCEPTION_INIT 3 Handle the raised exception Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Trapping NonĆPredefined Oracle7 Server Exceptions Trap a non-predefined Oracle7 Server error by declaring it... 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 Error Handling 25Ć19 25Ć20 Introduction to Oracle: SQL and 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 Error Handling 25Ć23 25Ć24... 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 2 is the name of the exception Associate the declared exception with the standard Oracle7 Server error number using the pragma EXCEPTION_INIT statement Syntax PRAGMA EXCEPTION_INIT (exception, error_ number);...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 . Error Handling 25 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder25Ć2 Error Handling 25Ć3 Objectives When. Procedure Builder25Ć8 Error Handling 25Ć9 Trapping Exceptions Trap any error by including a corresponding routine within the exception handling section of

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