Copyright  Oracle Corporation, 1999. All rights reserved. 23 Handling Exceptions.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

Chapter 4B: More Advanced PL/SQL Programming
Creating Triggers.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
PL/SQL Agenda: Basic PL/SQL block structure
Programming in Oracle with PL/SQL
Exception types In Oracle PL/SQL. Types of exceptions Named system exceptions –Raised as a result of an error in PL/SQL or RDBMS processing. Named programmer-defined.
Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
PL/SQL block has the following structure: DECLARE Declaration statements BEGIN Executable statements EXCEPTION Exception-handling statements END ;
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Bordoloi and Bock EXCEPTIONS. Bordoloi and Bock Errors Two types of errors can be found in a program: compilation errors and runtime errors. There is.
Cursor and Exception Handling By Nidhi Bhatnagar.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
11 Copyright س Oracle Corporation, All rights reserved. ® Overview of PL/SQL.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and query statements of SQL are included.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
L/O/G/O Working with Composite Data Types. Objectives After completing this lesson, you should be able to do the following: –Create user-defined PL/SQL.
CSIT 313 DB PROGRAMMING EXCEPTION HANDLING. In PL/SQL, an error condition is called an exception. An exception can be either –internally defined (by the.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
Oracle PL/SQL. PL/SQL Originally modeled after ADA Originally modeled after ADA Created for Dept. of DefenseCreated for Dept. of Defense Allows expanded.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Handling Exceptions. 2 home back first prev next last What Will I Learn? Describe several advantages of including exception handling code in PL/SQL Describe.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Oracle 8i Exception Handling. General Syntax DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2.
Using SQL in PL/SQL ITEC 224 Database Programming.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
Oracle 数据库应用 -- PL/SQL 进阶 (3) & Oracle DBA 2016/5/ /5/10.
DB Programming Course Lecture 4. Errors Two types of errors can be found in a program: compilation errors and runtime errors. There is a special section.
Preface IIntroduction Course Objectives I-2 Oracle Complete Solution I-3 Course Agenda I-4 Tables Used in This Course I-5 The Order Entry Schema I-6 The.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Free Powerpoint Templates Page 1 Free Powerpoint Templatesبسم الله الرحمن الرحيم عدد الساعات: 2 نظري+2عملي الرمز:314 حسب المتطلبات:223 حسب (مبادئ قواعد.
ITEC 224 Database Programming
CHAPTER 5 EXCEPTION HANDLING
Difference between Oracle PL/SQL and MySQL
Interacting with the Oracle Server
Handling Exceptions.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.
Handling Exceptions.
Database Management Systems 2
Agenda Summary of last class Cursors Loops Records Simple Loops
PL/SQL Scripting in Oracle:
Handling Exceptions.
PRACTICE OVERVIEW PL/SQL Part - 2.
Handling Exceptions.
Database Management Systems 2
Handling Exceptions.
PRACTICE OVERVIEW PL/SQL Part - 1.
Database Programming Using Oracle 11g
Presentation transcript:

Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions

23-2 Copyright  Oracle Corporation, 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 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

23-3 Copyright  Oracle Corporation, All rights reserved. Handling Exceptions with PL/SQL What is an exception? 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. What is an exception? 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.

23-4 Copyright  Oracle Corporation, All rights reserved. Handling Exceptions Trap the exception DECLARE BEGIN END; Exception is raised EXCEPTION Exception is trapped Propagate the exception DECLARE BEGIN END; Exception is raised EXCEPTION Exception is not trapped Exception propagates to calling environment

23-5 Copyright  Oracle Corporation, All rights reserved. Exception Types Predefined Oracle Server Non-predefined Oracle Server User-defined Predefined Oracle Server Non-predefined Oracle Server User-defined} Implicitly raised Explicitly raised

23-6 Copyright  Oracle Corporation, All rights reserved. Trapping Exceptions EXCEPTION WHEN exception1 [OR exception2...] THEN statement1; statement2;... [WHEN exception3 [OR exception4...] THEN statement1; statement2;...] [WHEN OTHERS THEN statement1; statement2;...] EXCEPTION WHEN exception1 [OR exception2...] THEN statement1; statement2;... [WHEN exception3 [OR exception4...] THEN statement1; statement2;...] [WHEN OTHERS THEN statement1; statement2;...] SyntaxSyntax

23-7 Copyright  Oracle Corporation, All rights reserved. Trapping Exceptions Guidelines WHEN OTHERS is the last clause. 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. EXCEPTION keyword starts exception- handling section. Several exception handlers are allowed. Only one handler is processed before leaving the block.

23-8 Copyright  Oracle Corporation, 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 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

23-9 Copyright  Oracle Corporation, All rights reserved.

23-10 Copyright  Oracle Corporation, All rights reserved. Predefined Exception BEGIN EXCEPTION WHEN NO_DATA_FOUND THEN statement1; statement2; WHEN TOO_MANY_ROWS THEN statement1; WHEN OTHERS THEN statement1; statement2; statement3; END; SyntaxSyntax

23-11 Copyright  Oracle Corporation, All rights reserved. Trapping Non-Predefined Oracle Server Errors Declare Name the exception Name the exception Associate Code the PRAGMA EXCEPTION_INIT Code the PRAGMA EXCEPTION_INIT Declarative section Reference Handle the raised exception Handle the raised exception Exception-handlingsection

23-12 Copyright  Oracle Corporation, All rights reserved. DECLARE e_emps_remainingEXCEPTION; PRAGMA EXCEPTION_INIT ( e_emps_remaining, -2292); v_deptno dept.deptno%TYPE := &p_deptno; BEGIN DELETE FROM dept WHERE deptno = v_deptno; COMMIT; EXCEPTION WHEN e_emps_remaining THEN DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' || TO_CHAR(v_deptno) || '. Employees exist. '); END; DECLARE e_emps_remainingEXCEPTION; PRAGMA EXCEPTION_INIT ( e_emps_remaining, -2292); v_deptno dept.deptno%TYPE := &p_deptno; BEGIN DELETE FROM dept WHERE deptno = v_deptno; COMMIT; EXCEPTION WHEN e_emps_remaining THEN DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' || TO_CHAR(v_deptno) || '. Employees exist. '); END; Non-Predefined Error Trap for Oracle Server error number 2292, an integrity constraint violation. Trap for Oracle Server error number –2292, an integrity constraint violation. e_emps_remaining EXCEPTION; 1 PRAGMA EXCEPTION_INIT ( e_emps_remaining, -2292); 2 e_emps_remaining 3

23-13 Copyright  Oracle Corporation, All rights reserved. Functions for Trapping Exceptions SQLCODE Returns the numeric value for the error code SQLERRM Returns the message associated with the error number SQLCODE Returns the numeric value for the error code SQLERRM Returns the message associated with the error number

23-14 Copyright  Oracle Corporation, All rights reserved. Functions for Trapping Exceptions 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 errors VALUES(v_error_code, v_error_message); END; ExampleExample SQLCODE SQLERRM

23-15 Copyright  Oracle Corporation, All rights reserved. Trapping User-Defined Exceptions Name the exception Name the exception Declare Declarativesection Raise Explicitly raise the exception by using the RAISE statement Explicitly raise the exception by using the RAISE statement Executablesection Reference Handle the raised exception Handle the raised exception Exception-handlingsection

23-16 Copyright  Oracle Corporation, All rights reserved. User-Defined Exception DECLARE e_invalid_product EXCEPTION; BEGIN UPDATEproduct SETdescrip = '&product_description' WHEREprodid = &product_number; IF SQL%NOTFOUND THEN RAISE e_invalid_product; END IF; COMMIT; EXCEPTION WHEN e_invalid_product THEN DBMS_OUTPUT.PUT_LINE('Invalid product number.'); END; ExampleExample e_invalid_product EXCEPTION; 1 RAISE e_invalid_product; 2 e_invalid_product 3

23-17 Copyright  Oracle Corporation, All rights reserved. Calling Environments SQL*Plus Procedure Builder Oracle Developer Forms Precompiler application An enclosing PL/SQL block Displays error number and message to screen Accesses error number and message in a trigger by means of the ERROR_CODE and ERROR_TEXT packaged functions Accesses exception number through the SQLCA data structure Traps exception in exception- handling routine of enclosing block

23-18 Copyright  Oracle Corporation, All rights reserved. Propagating Exceptions BEGIN SELECT... UPDATE... IF SQL%NOTFOUND THEN RAISE e_no_rows; END IF; EXCEPTION WHEN e_integrity THEN... WHEN e_no_rows THEN... END; DECLARE... e_no_rowsexception; e_integrityexception; PRAGMA EXCEPTION_INIT (e_integrity, -2292); BEGIN FOR c_record IN emp_cursor LOOP 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. BEGIN SELECT... UPDATE... IF SQL%NOTFOUND THEN RAISE e_no_rows; END IF; EXCEPTION WHEN e_integrity THEN... WHEN e_no_rows THEN... END;

23-19 Copyright  Oracle Corporation, All rights reserved. RAISE_APPLICATION_ERROR Procedure Syntax A procedure that lets you issue user- defined error messages from stored subprograms Called only from an executing stored subprogramSyntax A procedure that lets you issue user- defined error messages from stored subprograms Called only from an executing stored subprogram raise_application_error (error_number, message[, {TRUE | FALSE}]);

23-20 Copyright  Oracle Corporation, All rights reserved. RAISE_APPLICATION_ERROR Procedure Used in two different places: – Executable section – Exception section Returns error conditions to the user in a manner consistent with other Oracle Server errors Used in two different places: – Executable section – Exception section Returns error conditions to the user in a manner consistent with other Oracle Server errors

23-21 Copyright  Oracle Corporation, All rights reserved. Summary Exception types: – Predefined Oracle Server error – Non-predefined Oracle Server error – User-defined error Exception trapping Exception handling: – Trap the exception within the PL/SQL block. – Propagate the exception. Exception types: – Predefined Oracle Server error – Non-predefined Oracle Server error – User-defined error Exception trapping Exception handling: – Trap the exception within the PL/SQL block. – Propagate the exception.

23-22 Copyright  Oracle Corporation, All rights reserved. Practice Overview Handling named exceptions Creating and invoking user-defined exceptions Handling named exceptions Creating and invoking user-defined exceptions

23-23 Copyright  Oracle Corporation, All rights reserved.

23-24 Copyright  Oracle Corporation, All rights reserved.