Beruflich Dokumente
Kultur Dokumente
Handling Exceptions
Objectives
After , you lesson After completing completing this this lesson, lesson, you should should be able to do the following: : following be able to do the following: Define Define PL/SQL PL/SQL exceptions exceptions Recognize Recognize unhandled unhandled exceptions exceptions List List and and use use different different types types of of PL/SQL PL/SQL exception exception handlers handlers Trap Trap unanticipated unanticipated errors errors Describe Describe the the effect effect of of exception exception propagation propagation in in nested nested blocks blocks Customize Customize PL/SQL PL/SQL exception exception messages messages
23-2 Copyright Oracle Corporation, 1998. All rights reserved.
How How do do you you handle handle it? it? Trap Trap it it with with a a handler. handler.
Propagate Propagate it it to to the the calling calling environment. environment.
23-3 Copyright Oracle Corporation, 1998. All rights reserved.
Handling Exceptions
Trap the Exception
DECLARE BEGIN
Exception is raised Exception is trapped
EXCEPTION END;
Exception Types
Predefined Predefined Oracle Oracle Server Server Non-predefined Non-predefined Oracle Oracle Server Server User-defined User-defined
Implicitly raised
Explicitly raised
23-5
Trapping Exceptions
Syntax Syntax
EXCEPTION EXCEPTION WHEN WHEN exception1 exception1 [OR [OR exception2 exception2 . .. . .] .] THEN THEN statement1 ; statement1 ; statement2 ; statement2 ; . . . . . . [WHEN [WHEN exception3 exception3 [OR [OR exception4 exception4 . .. . .] .] THEN THEN statement1 ; statement1; statement2 ; statement2 ; . .. . .] .] [WHEN [WHEN OTHERS OTHERS THEN THEN statement1 ; statement1 ; statement2 ; statement2 ; . . .] . . .]
23-6
23-7
23-9
Predefined Exception
Syntax Syntax
BEGIN SELECT ... COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN statement1; statement2; WHEN TOO_MANY_ROWS THEN statement1; WHEN OTHERS THEN statement1; statement2; statement3; END;
23-10
Declarative Section
Name the
exception
23-11
Non-Predefined Error
Trap Trap for for Oracle Oracle Server Server error error number number 2292 an -2292 an integrity integrity constraint constraint violation violation
DECLARE DECLARE e_products_invalid EXCEPTION; e_products_invalidEXCEPTION; e_products_invalidEXCEPTION; PRAGMA EXCEPTION_INIT ( PRAGMA EXCEPTION_INIT ( PRAGMA EXCEPTION_INIT ( e_products_invalid, e_products_invalid, -2292); e_products_invalid, -2292); -2292); v_message v_message VARCHAR2(50); VARCHAR2(50); BEGIN BEGIN . .. .. . EXCEPTION EXCEPTION WHEN WHEN e_products_invalid e_products_invalid THEN THEN :g_message :g_message := := 'Product 'Product code code specified specified is is not not valid.'; valid.'; . .. .. . END; END;
23-12 Copyright Oracle Corporation, 1998. All rights reserved.
1 2
23-13
Raise
Executable Section
Reference
ExceptionException-Handling Section
Name the
exception
Explicitly raise
the exception by using the RAISE statement
Handle the
raised exception
23-14
User-Defined Exception
Example Example
[DECLARE] [DECLARE] e_amount_remaining EXCEPTION; e_amount_remaining EXCEPTION; e_amount_remaining EXCEPTION; . . . . . . BEGIN BEGIN . .. .. . RAISE e_amount_remaining; RAISE e_amount_remaining; RAISE e_amount_remaining; . .. .. . EXCEPTION EXCEPTION e_amount_remaining WHEN WHEN e_amount_remaining e_amount_remaining THEN THEN :g_message :g_message := := 'There 'There is is still still an an amount amount in in stock.'; stock.'; . .. .. . END; END; 1
23-15
23-16
SQLCODE ; := SQLERRM ;
Calling Environments
SQL*Plus Procedure Builder Displays error number and message to screen Displays error number and message to screen
Accesses error number and message Developer/2000 in a trigger by means of the Forms ERROR_CODE and ERROR_TEXT packaged functions Precompiler application An enclosing PL/SQL block
23-18
Accesses exception number through the SQLCA data structure Traps exception in exceptionhandling routine of enclosing block
Propagating Exceptions
DECLARE . . . e_no_rows exception; e_integrity exception; PRAGMA EXCEPTION_INIT (e_integrity, -2292); BEGIN FOR c_record IN emp_cursor LOOP BEGIN SELECT ... UPDATE ... IF SQL%NOTFOUND THEN RAISE e_no_rows; END IF; EXCEPTION WHEN e_integrity THEN ... WHEN e_no_rows THEN ... END; END LOOP; EXCEPTION WHEN NO_DATA_FOUND THEN . . . WHEN TOO_MANY_ROWS THEN . . . END;
Subblocks can handle an exception or pass the exception to the enclosing block. block.
23-19
Summary
Exception Exception types: types: Predefined Predefined Oracle Oracle Server Server error error Non-predefined Non-predefined Oracle Oracle Server Server error error User-defined User-defined error error Trap Trap exceptions exceptions Handle Handle exceptions: exceptions: Trap Trap the the exception exception within within the the PL/SQL PL/SQL block block Propagate Propagate the the exception exception
23-20 Copyright Oracle Corporation, 1998. All rights reserved.
Practice Overview
Handling Handling named named exceptions exceptions Creating Creating and and invoking invoking user-defined user-defined exceptions exceptions
23-21
Practice Overview
Handling Handling named named exceptions exceptions Creating Creating and and invoking invoking user-defined user-defined exceptions exceptions
23-22
23-23