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.

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
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
PL/SQL Agenda: Basic PL/SQL block structure
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.
Introduction to PL/SQL
Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.
PL/SQL block has the following structure: DECLARE Declaration statements BEGIN Executable statements EXCEPTION Exception-handling statements END ;
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
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.
Benefits of PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –List and explain the benefits of PL/SQL –List.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
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.
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.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Recognizing PL/SQL Lexical Units. 2 home back first prev next last What Will I Learn? List and define the different types of lexical units available in.
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.
Oracle 8i Exception Handling. General Syntax DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2.
Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Using SQL in PL/SQL ITEC 224 Database Programming.
Using Oracle-Supplied Packages. 2 home back first prev next last What Will I Learn? Describe two common uses for the DBMS_OUTPUT server-supplied package.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
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 
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Cursor FOR Loops. 2 home back first prev next last What Will I Learn? List and explain the benefits of using cursor FOR loops Create PL/SQL code to declare.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
implicit and an explicit cursor
Using Functions in SQL Statements. 2 home back first prev next last What Will I Learn? List the advantages of user-defined functions in SQL statements.
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.
Passing Parameters. 2 home back first prev next last What Will I Learn? List the types of parameter modes Create a procedure that passes parameters Identify.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
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.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
ITEC 224 Database Programming
CHAPTER 5 EXCEPTION HANDLING
Interacting with the Oracle Server
Creating Stored Procedures and Functions
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 Programming PL SQL.
Database Management Systems 2
Agenda Summary of last class Cursors Loops Records Simple Loops
Handling Exceptions.
Handling Exceptions.
Chapter 4: Introduction to PL/SQL
Database Management Systems 2
Handling Exceptions.
Database Programming Using Oracle 11g
Presentation transcript:

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 server. Describe and provide an example of an error defined by the PL/SQL programmer Differentiate between errors that are handled implicitly and explicitly by the Oracle Server Write PL/SQL code to trap a predefined Oracle Server error Write PL/SQL code to trap a non-predefined Oracle Server error Write PL/SQL code to identify an exception by error code and by error message

3 home back first prev next last Why Learn It? PL/SQL error handling is very flexible and allows programmers to use both errors defined by the Oracle server and errors defined by the programmer. This lesson discusses predefined and nonpredefined Oracle Server errors. –Predefined errors are the common Oracle errors for which PL/SQL has predefined exception names. –Non-predefined errors make use of the ORA error codes and messages. –The syntax is different for each, but you can trap both kinds of errors in the EXCEPTION section of your PL/SQL program.

4 home back first prev next last Exception Types

5 home back first prev next last Handling Exceptions with PL/SQL There are two methods for raising an exception – Implicitly (automatically) by the Oracle Server:  An Oracle error occurs and the associated exception is raised automatically. For example, if the error ORA occurs when no rows are retrieved from the database in a SELECT statement, then PL/SQL raises the exception NO_DATA_FOUND. – Explicitly by the programmer:  Depending on the business functionality your program is implementing, you may have to explicitly raise an exception.  You raise an exception explicitly by issuing the RAISE statement within the block.  The exception being raised may be either user-defined or predefined.  These are explained in the next lesson.

6 home back first prev next last Two Types of Oracle Server Error When an Oracle Server error occurs, the Oracle server automatically raises the associated exception, skips the rest of the executable section of the block, and looks for a handler in the exception section. There are two types of Oracle Server error: –Predefined Oracle Server Errors:  Each of these has a predefined name. For example, if the error ORA occurs when no rows are retrieved from the database in a SELECT statement, then PL/SQL raises the predefined exception-name NO_DATA_FOUND. –Non-predefined Oracle Server Errors:  Each of these has a standard Oracle error number (ORA- nnnnn) and error message, but not a predefined name.  We declare our own names for these so that we can reference these names in the exception section.

7 home back first prev next last Trapping Predefined Oracle Server Errors Reference the predefined name in the exception handling routine. Sample predefined exceptions: –NO_DATA_FOUND –TOO_MANY_ROWS –INVALID_CURSOR –ZERO_DIVIDE –DUP_VAL_ON_INDEX For a partial list of predefined exceptions refer to plsql_s06_l02_predefined_errors.doc. For a complete list of predefined exceptions, see PL/SQL User’s Guide and Reference.

8 home back first prev next last Trapping Predefined Oracle Server Errors The following example uses the TOO_MANY_ROWS predefined Oracle Server error. Note that it is not declared in the DECLARATION section. DECLARE v_lname VARCHAR2(15); BEGIN SELECT last_name INTO v_lname FROM employees WHERE first_name like 'El%'; DBMS_OUTPUT.PUT_LINE('Ellen''s last name is : ' || v_lname); EXCEPTION WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE(' Your select statement retrieved multiple rows. Consider using a cursor.'); END;

9 home back first prev next last Trapping Several Predefined Oracle Server Errors This example handles TOO_MANY_ROWS and NO_DATA_FOUND, with an OTHERS handler in case any other error occurs. DECLARE v_lname VARCHAR2(15); BEGIN SELECT last_name INTO v_lname FROM employees WHERE first_name = 'John'; DBMS_OUTPUT.PUT_LINE('John''s last name is : ' || v_lname); EXCEPTION WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE('Select statement found multiple rows'); WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Select statement found no rows'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Another type of error occurred'); END;

10 home back first prev next last Trapping Non-Predefined Oracle Server Errors Non-predefined exceptions are similar to predefined exceptions; –however, they do not have predefined names in PL/SQL. –They are standard Oracle Server errors and have ORA- error numbers. –You create your own names for them in the DECLARE section and associate these names with ORA- error numbers using the PRAGMA EXCEPTION_INIT function.

11 home back first prev next last Trapping Non-Predefined Oracle Server Errors You can trap a non-predefined Oracle server error by declaring it first. –The declared exception is raised implicitly. –In PL/SQL, the PRAGMA EXCEPTION_INIT tells the compiler to associate an exception name with an Oracle error number. This allows you to refer to any Oracle Server exception by name and to write a specific handler for it.

12 home back first prev next last Non-Predefined Error Examine the following example. BEGIN INSERT INTO departments (department_id, department_name) VALUES (280, NULL); END; The INSERT statement tries to insert the value NULL for the department_name column of the departments table. –However, the operation is not successful because department_name is a NOT NULL column. –There is no predefined error name for violating a NOT NULL constraint. The way to work around this problem is to declare our own name and associate it with the ORA-1400 error.

13 home back first prev next last Non-Predefined Error 1. Declare the name of the exception in the declarative section.

14 home back first prev next last Non-Predefined Error 2. Associate the declared exception with the standard Oracle server error number using the PRAGMA EXCEPTION_INIT function.

15 home back first prev next last Non-Predefined Error 3. Reference the declared exception name within the corresponding exception-handling routine

16 home back first prev next last Functions for Trapping Exceptions When an exception occurs, you can retrieve the associated error code or error message by using two functions. –Based on the values of the code or the message, you can decide which subsequent actions to take.  SQLERRM returns character data containing the message associated with the error number  SQLCODE returns the numeric value for the error code (You can assign it to a NUMBER variable.)

17 home back first prev next last Functions for Trapping Exceptions You cannot use SQLCODE or SQLERRM directly in a SQL statement. –Instead, you must assign their values to local variables, then use the variables in the SQL statement, as shown in the following example:

18 home back first prev next last Terminology Key terms used in this lesson include: –Predefined Oracle Server Errors –Non-predefined Oracle Server Errors –PRAGMA EXCEPTION_INIT –SQLERRM –SQLCODE

19 home back first prev next last Summary In this lesson, you learned to: –Describe and provide an example of an error defined by the Oracle server. –Describe and provide an example of an error defined by the PL/SQL programmer –Differentiate between errors that are handled implicitly and explicitly by the Oracle Server –Write PL/SQL code to trap a predefined Oracle Server error –Write PL/SQL code to trap a non-predefined Oracle Server error –Write PL/SQL code to identify an exception by error code and by error message

20 home back first prev next last Try It / Solve It The exercises in this lesson cover the following topics: –Listing and describing different types of PL/SQL exception handlers –Differentiating between errors that are handled implicitly and explicitly by the Oracle Server –Trapping predefined Oracle Server errors –Trapping non-predefined Oracle Server errors