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.

Slides:



Advertisements
Similar presentations
ORACLE TRANSACTIONS A transaction begins with the first executable SQL statement after a commit, rollback or connection made to the Oracle engine. All.
Advertisements

Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
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:
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 PL/SQL : INTRODUCTION. Bordoloi and BockPL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational.
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.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
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.
Slides Credit Umair Javed LUMS Web Application Development.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
University of Sunderland COM 220 Lecture Six Slide 1 Building Interactive Forms Applications using Oracle.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
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.
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.
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.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
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.
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 
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
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.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
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
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
Advanced PL/SQL Programing
Handling Exceptions.
Handling Exceptions.
Database Management Systems 2
Handling Exceptions.
Using Subqueries to Solve Queries
Presentation transcript:

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 the purpose of an EXCEPTION section in a PL/SQL block Create PL/SQL code to include an EXCEPTION section List several guidelines for exception handling

3 home back first prev next last Why Learn It? You have learned to write PL/SQL blocks with a declarative section and an executable section. All the SQL and PL/SQL code that has to be executed is written in the executable block. So far we have assumed that the code works fine if we take care of compile time errors. However, the code may cause some unanticipated errors at run time. In this lesson, you learn how to deal with such errors in the PL/SQL block.

4 home back first prev next last What is an Exception? An exception occurs when an error is discovered during the execution of a program that disrupts the normal operation of the program. There are many possible causes of exceptions: a user makes a spelling mistake while typing; a program does not work correctly; an advertised web page does not exist; and so on. Can you think of errors that you have come across while using a website or application?

5 home back first prev next last Exceptions in PL/SQL This example works fine. But what if you were to enter Korea, South instead of Republic of Korea? DECLARE v_country_name wf_countries.country_name%TYPE := 'Republic of Korea'; v_elevation wf_countries.highest_elevation%TYPE; BEGIN SELECT highest_elevation INTO v_elevation FROM wf_countries WHERE country_name = v_country_name; DBMS_OUTPUT.PUT_LINE(v_country_name); END;

6 home back first prev next last Exceptions in PL/SQL DECLARE v_country_name wf_countries.country_name%TYPE := 'Republic of Korea'; v_elevation wf_countries.highest_elevation%TYPE; BEGIN SELECT highest_elevation INTO v_elevation FROM wf_countries WHERE country_name = v_country_name; DBMS_OUTPUT.PUT_LINE(v_country_name); END;

7 home back first prev next last Exceptions in PL/SQL Observe that the code does not work as expected. No data was found for Korea, South because the country name is actually stored as Republic of Korea. This type of error in PL/SQL is called an exception. –When an exception occurs, we say that the exception has been “raised”. –When an exception is raised, the rest of the execution section of the PL/SQL block is not executed.

8 home back first prev next last What is an Exception Handler? An exception handler is code that defines the recovery actions to be performed when an exception is raised (i.e. when an error occurs). When writing code, –programmers need to anticipate the types of errors that can occur during execution of that code. –They need to include exception handlers in their code to address these errors. –In a sense, exception handlers allow programmers to "bulletproof" their code.

9 home back first prev next last What is an Exception Handler? What types of errors may programmers want to account for by using an exception handler? –System errors (e.g., a hard disk is full) –Data errors (e.g., trying to duplicate a primary key value) –User action errors (e.g., data entry error) –Many other possibilities !

10 home back first prev next last Why is Exception Handling Important? Can you think of some reasons why exception handling is important?

11 home back first prev next last Why is Exception Handling Important? Some reasons include: –Protects the user from errors (frequent errors may frustrate the user and/or cause the user to quit the application) –Protects the database from errors (data can be lost or overwritten) –Major errors take a lot of system resources (if a mistake is made, correcting the mistake may be costly; users may frequently call the help desk for assistance with errors). –Code is more readable because error-handling routines can be written in the same block in which the error occurred.

12 home back first prev next last Handling Exceptions with PL/SQL A block always terminates when PL/SQL raises an exception, –but you can specify an exception handler to perform final actions before the block ends. –The exception section begins with the keyword EXCEPTION.

13 home back first prev next last Handling Exceptions with PL/SQL When an exception is handled, the PL/SQL program does not terminate abruptly. –When the exception is raised, the control shifts to the exception section and the handler in the exception section is executed. –The PL/SQL block terminates with normal, successful completion. –Only one exception can occur at a time. When an exception occurs, PL/SQL processes only one handler before leaving the block.

14 home back first prev next last Handling Exceptions with PL/SQL The code at point A will not execute because the SELECT statement failed.

15 home back first prev next last Handling Exceptions with PL/SQL Below is another example. The select statement in the block is retrieving the last_name of John. 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); END; However, an exception is raised because more than one John exists in the data.

16 home back first prev next last Handling Exceptions with PL/SQL The following code includes a handler for a predefined Oracle server error called TOO_MANY_ROWS. You will learn more about predefined server errors in the next lesson. DECLARE v_lname employees.last_name%TYPE; 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;

17 home back first prev next last Trapping Exceptions You can handle or "trap" any error by including a corresponding handler within the exception handling section of the PL/SQL block. Syntax:

18 home back first prev next last Trapping Exceptions Each handler consists of a WHEN clause, which specifies an exception name, followed by a sequence of statements to be executed when that exception is raised. You can include any number of handlers within an EXCEPTION section to handle specific exceptions. However, you cannot have multiple handlers for a single exception.

19 home back first prev next last Trapping Exceptions In the syntax: –exception Is the standard name of a predefined exception or the name of a user-defined exception declared within the declarative section –statement Is one or more PL/SQL or SQL statements –OTHERS Is an optional exception-handling clause that traps any exceptions that have not been explicitly handled

20 home back first prev next last The OTHERS Exception Handler The exception-handling section traps only those exceptions that are specified; any other exceptions are not trapped unless you use the OTHERS exception handler. The OTHERS handler traps all the exceptions that are not already trapped. If used, OTHERS must be the last exception handler that is defined.

21 home back first prev next last The OTHERS Exception Handler Consider the following example: –If the exception NO_DATA_FOUND is raised by the program, then the statements in the corresponding handler are executed. –If the exception TOO_MANY_ROWS is raised, then the statements in the corresponding handler are executed. –However, if some other exception is raised, then the statements in the OTHERS exception handler are executed.

22 home back first prev next last Guidelines for Trapping Exceptions Always add exception handlers whenever there is a possibility of an error occurring. Errors are especially likely during calculations, string manipulation, and SQL database operations. Handle named exceptions whenever possible, instead of using OTHERS in exception handlers. Learn the names and causes of the predefined exceptions. Test your code with different combinations of bad data to see what potential errors arise. Write out debugging information in your exception handlers. Carefully consider whether each exception handler should commit the transaction, roll it back, or let it continue. No matter how severe the error is, you want to leave the database in a consistent state and avoid storing any bad data.

23 home back first prev next last Terminology Key terms used in this lesson include: –Exception –Exception Handler

24 home back first prev next last Summary In this lesson, you learned to: –Describe several advantages of including exception handling code in PL/SQL –Describe the purpose of an EXCEPTION section in a PL/SQL block –Create PL/SQL code to include an EXCEPTION section –List several guidelines for exception handling

25 home back first prev next last Try It / Solve It The exercises in this lesson cover the following topics: –Explaining the importance of exception handling –Describing what an exception is and how to handle it in PL/SQL programs –Creating PL/SQL code to include an EXCEPTION section –Following guidelines for exception handling