Sie sind auf Seite 1von 12

23

Handling Exceptions

Copyright Oracle Corporation, 1998. All rights reserved.

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.

PBD Oracle - Exceptii-1

Handling Exceptions with PL/SQL


What What is is an an exception? exception? Identifier Identifier in in PL/SQL PL/SQL that that is is raised raised during during execution. execution. How How is is it it raised? raised?
An An Oracle Oracle error error occurs. occurs. You You raise raise it it explicitly. explicitly.

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

Propagate the Exception


DECLARE BEGIN EXCEPTION END;

EXCEPTION END;

Exception is raised Exception is not trapped

Exception propagates to calling environment


23-4 Copyright Oracle Corporation, 1998. All rights reserved.

PBD Oracle - Exceptii-2

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

Copyright Oracle Corporation, 1998. All rights reserved.

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

Copyright Oracle Corporation, 1998. All rights reserved.

PBD Oracle - Exceptii-3

Trapping Exceptions Guidelines


WHEN WHEN OTHERS OTHERS is is the the last last clause. clause. EXCEPTION EXCEPTION keyword keyword starts starts exceptionexceptionhandling handling section. section. Several Several exception exception handlers handlers are are allowed. allowed. Only Only one one handler handler is is processed processed before before leaving leaving the the block. block.

23-7

Copyright Oracle Corporation, 1998. All rights reserved.

Trapping Predefined Oracle Server Errors


Reference Reference the the standard standard name name in in the the exception-handling exception-handling routine. routine. Sample Sample predefined predefined exceptions: exceptions:
NO_DATA_FOUND NO_DATA_FOUND TOO_MANY_ROWS TOO_MANY_ROWS INVALID_CURSOR INVALID_CURSOR ZERO_DIVIDE ZERO_DIVIDE DUP_VAL_ON_INDEX DUP_VAL_ON_INDEX
23-8 Copyright Oracle Corporation, 1998. All rights reserved.

PBD Oracle - Exceptii-4

23-9

Copyright Oracle Corporation, 1998. All rights reserved.

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

Copyright Oracle Corporation, 1998. All rights reserved.

PBD Oracle - Exceptii-5

Trapping Non-Predefined Oracle Server Errors


Declare Associate Reference
ExceptionException-Handling Section

Declarative Section

Name the
exception

Code the PRAGMA Handle the


EXCEPTION_INIT raised exception

23-11

Copyright Oracle Corporation, 1998. All rights reserved.

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

PBD Oracle - Exceptii-6

23-13

Copyright Oracle Corporation, 1998. All rights reserved.

Trapping User-Defined Exceptions


Declare
Declarative Section

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

Copyright Oracle Corporation, 1998. All rights reserved.

PBD Oracle - Exceptii-7

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

Copyright Oracle Corporation, 1998. All rights reserved.

Functions for Trapping Exceptions


SQLCODE SQLCODE Returns Returns the the numeric numeric value value for for the the error error code code SQLERRM SQLERRM Returns Returns the the message message associated associated with with the the error error number number

23-16

Copyright Oracle Corporation, 1998. All rights reserved.

PBD Oracle - Exceptii-8

Functions for Trapping Exceptions


Example Example
DECLARE v_error_code v_error_message BEGIN ... EXCEPTION ... WHEN OTHERS THEN ROLLBACK; v_error_code := v_error_message NUMBER; VARCHAR2(255);

SQLCODE ; := SQLERRM ;

INSERT INTO errors VALUES(v_error_code, v_error_message); END;


23-17 Copyright Oracle Corporation, 1998. All rights reserved.

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

Copyright Oracle Corporation, 1998. All rights reserved.

PBD Oracle - Exceptii-9

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

Copyright Oracle Corporation, 1998. All rights reserved.

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.

PBD Oracle - Exceptii-10

Practice Overview
Handling Handling named named exceptions exceptions Creating Creating and and invoking invoking user-defined user-defined exceptions exceptions

23-21

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-22

Copyright Oracle Corporation, 1998. All rights reserved.

PBD Oracle - Exceptii-11

23-23

Copyright Oracle Corporation, 1998. All rights reserved.

PBD Oracle - Exceptii-12

Das könnte Ihnen auch gefallen