oracle - program with pl/sql - lession 08

23
8 Copyright © Oracle Corporation, 2001. All rights reserved. Handling Exceptions

Upload: thuan-nguyen

Post on 22-Jan-2018

3.071 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Oracle - Program with PL/SQL - Lession 08

8Copyright © Oracle Corporation, 2001. All rights reserved.

Handling Exceptions

Page 2: Oracle - Program with PL/SQL - Lession 08

8-2 Copyright © Oracle Corporation, 2001. All rights reserved.

Objectives

After completing this lesson, you should be able to

do the following:

• Define PL/SQL exceptions

• Recognize unhandled exceptions

• List and use different types of PL/SQL exception

handlers

• Trap unanticipated errors

• Describe the effect of exception propagation in

nested blocks

• Customize PL/SQL exception messages

Page 3: Oracle - Program with PL/SQL - Lession 08

8-3 Copyright © Oracle Corporation, 2001. All rights reserved.

Handling Exceptions with PL/SQL

• An exception is an identifier in PL/SQL that is raised during execution.

• How is it raised?

– An Oracle error occurs.

– You raise it explicitly.

• How do you handle it?

– Trap it with a handler.

– Propagate it to the calling environment.

Page 4: Oracle - Program with PL/SQL - Lession 08

8-4 Copyright © Oracle Corporation, 2001. All rights reserved.

Handling Exceptions

Page 5: Oracle - Program with PL/SQL - Lession 08

8-5 Copyright © Oracle Corporation, 2001. All rights reserved.

Exception Types

• Predefined Oracle Server

• Nonpredefined Oracle Server

• User-defined Explicitly raised

Page 6: Oracle - Program with PL/SQL - Lession 08

8-6 Copyright © Oracle Corporation, 2001. All rights reserved.

Trapping Exceptions

Syntax:

EXCEPTION WHEN exception1 [OR exception2 . . .] THEN

statement1;statement2;. . .

[WHEN exception3 [OR exception4 . . .] THENstatement1;statement2;. . .]

[WHEN OTHERS THENstatement1;statement2;. . .]

Page 7: Oracle - Program with PL/SQL - Lession 08

8-7 Copyright © Oracle Corporation, 2001. All rights reserved.

Trapping Exceptions Guidelines

• The EXCEPTION keyword starts exception-handling

section.

• Several exception handlers are allowed.

• Only one handler is processed before leaving the

block.

• WHEN OTHERS is the last clause.

Page 8: Oracle - Program with PL/SQL - Lession 08

8-8 Copyright © Oracle Corporation, 2001. All rights reserved.

Trapping Predefined Oracle Server Errors

• 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

Page 9: Oracle - Program with PL/SQL - Lession 08

8-9 Copyright © Oracle Corporation, 2001. All rights reserved.

BEGIN. . .EXCEPTION WHEN NO_DATA_FOUND THEN

statement1;statement2;

WHEN TOO_MANY_ROWS THENstatement1;

WHEN OTHERS THENstatement1;statement2;statement3;

END;

Predefined Exceptions

Syntax:

Page 10: Oracle - Program with PL/SQL - Lession 08

8-10 Copyright © Oracle Corporation, 2001. All rights reserved.

Trapping Nonpredefined OracleServer Errors

Page 11: Oracle - Program with PL/SQL - Lession 08

8-11 Copyright © Oracle Corporation, 2001. All rights reserved.

DEFINE p_deptno = 10DECLARE

e_emps_remaining EXCEPTION;PRAGMA EXCEPTION_INIT (e_emps_remaining, -2292);

BEGINDELETE FROM departmentsWHERE department_id = &p_deptno;COMMIT;

EXCEPTIONWHEN e_emps_remaining THENDBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' ||TO_CHAR(&p_deptno) || '. Employees exist. ');

END;

Nonpredefined Error

Trap for Oracle server error number –2292, an

integrity constraint violation.

Page 12: Oracle - Program with PL/SQL - Lession 08

8-12 Copyright © Oracle Corporation, 2001. All rights reserved.

• SQLCODE: Returns the numeric value for the

error code

• SQLERRM: Returns the message associated

with the error number

Functions for Trapping Exceptions

Page 13: Oracle - Program with PL/SQL - Lession 08

8-13 Copyright © Oracle Corporation, 2001. All rights reserved.

Functions for Trapping Exceptions

Example:

DECLARE v_error_code NUMBER; v_error_message VARCHAR2(255);BEGIN...EXCEPTION... WHEN OTHERS THEN

ROLLBACK;v_error_code := SQLCODE ;v_error_message := SQLERRM ;INSERT INTO errorsVALUES(v_error_code, v_error_message);

END;

Page 14: Oracle - Program with PL/SQL - Lession 08

8-14 Copyright © Oracle Corporation, 2001. All rights reserved.

Trapping User-Defined Exceptions

Page 15: Oracle - Program with PL/SQL - Lession 08

8-15 Copyright © Oracle Corporation, 2001. All rights reserved.

User-Defined Exceptions

Page 16: Oracle - Program with PL/SQL - Lession 08

8-16 Copyright © Oracle Corporation, 2001. All rights reserved.

Calling Environments

Page 17: Oracle - Program with PL/SQL - Lession 08

8-17 Copyright © Oracle Corporation, 2001. All rights reserved.

Propagating Exceptions

Page 18: Oracle - Program with PL/SQL - Lession 08

8-18 Copyright © Oracle Corporation, 2001. All rights reserved.

raise_application_error (error_number, message[, {TRUE | FALSE}]);

The RAISE_APPLICATION_ERRORProcedure

Syntax:

• You can use this procedure to issue user-defined

error messages from stored subprograms.

• You can report errors to your application and

avoid returning unhandled exceptions.

Page 19: Oracle - Program with PL/SQL - Lession 08

8-19 Copyright © Oracle Corporation, 2001. All rights reserved.

The RAISE_APPLICATION_ERRORProcedure

• Used in two different places:

– Executable section

– Exception section

• Returns error conditions to the user in a manner

consistent with other Oracle server errors

Page 20: Oracle - Program with PL/SQL - Lession 08

8-20 Copyright © Oracle Corporation, 2001. All rights reserved.

RAISE_APPLICATION_ERROR

set serveroutput onDECLARE e_name EXCEPTION; PRAGMA EXCEPTION_INIT (e_name, -20999);--Executable section :BEGIN DELETE FROM employees WHERE last_name = 'Higginss'; IF SQL%NOTFOUND THEN RAISE_APPLICATION_ERROR(-20999,'This is not a valid last name'); END IF;-- Exception section : EXCEPTION WHEN e_name THEN dbms_output.put_line('handle the error');END;/

Page 21: Oracle - Program with PL/SQL - Lession 08

8-21 Copyright © Oracle Corporation, 2001. All rights reserved.

Summary

In this lesson, you should have learned that:

• Exception types:

– Predefined Oracle server error

– Nonpredefined Oracle server error

– User-defined error

• Exception trapping

• Exception handling:

– Trap the exception within the PL/SQL block.

– Propagate the exception.

Page 22: Oracle - Program with PL/SQL - Lession 08

8-22 Copyright © Oracle Corporation, 2001. All rights reserved.

Practice 8 Overview

This practice covers the following topics:

• Handling named exceptions

• Creating and invoking user-defined exceptions

Page 23: Oracle - Program with PL/SQL - Lession 08

8-23 Copyright © Oracle Corporation, 2001. All rights reserved.