PL/SQL block has the following structure: DECLARE Declaration statements BEGIN Executable statements EXCEPTION Exception-handling statements END ;

Slides:



Advertisements
Similar presentations
Oracle PL/SQL IV Exceptions Packages.
Advertisements

PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Chapter 4B: More Advanced PL/SQL Programming
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.
1 Agenda Summary of last class Loops Simple Loops WHILE Loops FOR Loops Records Cursors.
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
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.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQL : INTRODUCTION. PL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database language. With PL/SQL, you.
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.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
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:
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 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.
Distributed Database Applications COSC 5050 Week Five.
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.
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.
Using SQL in PL/SQL ITEC 224 Database Programming.
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 
Bordoloi and Bock CONTROL STRUCTURES: CONDITIONAL CONTROLS.
CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous.
implicit and 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.
Introduction to PL/SQL As usual, use speaker notes for additional information!
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
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.
PLSQL Cont…. Most Common Oracle Data Types VARCHAR2 –Stores variable-length character data. –Takes a required parameter that specifies a maximum length.
Free Powerpoint Templates Page 1 Free Powerpoint Templatesبسم الله الرحمن الرحيم عدد الساعات: 2 نظري+2عملي الرمز:314 حسب المتطلبات:223 حسب (مبادئ قواعد.
ITEC 224 Database Programming
CHAPTER 5 EXCEPTION HANDLING
DataBase Logic in Business Applications
Creating Stored Procedures and Functions
Interacting with the Oracle Server
Handling Exceptions.
SQL Stored Triggers Presented by: Dr. Samir Tartir
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.
Agenda Summary of last class Cursors Loops Records Simple Loops
Handling Exceptions.
Handling Exceptions.
Database Management Systems 2
Handling Exceptions.
Database Programming Using Oracle 11g
Presentation transcript:

PL/SQL block has the following structure: DECLARE Declaration statements BEGIN Executable statements EXCEPTION Exception-handling statements END ;

The exception-handling section is the last section of the PL/SQL block. This section contains statements that are executed when a runtime error occurs within a block. Runtime errors occur while the program is running and cannot be detected by the PL/SQL compiler. EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE (‘ There is no student with student id 123 ’); END;

 System exceptions Named Unnamed  User-defined exceptions

Oracle can handle: CURSOR_ALREADY_OPENED (sqlcode = -6511) DUP_VAL_ON_INDEX (sqlcode = -0001) INVALID_CURSOR (sqlcode = -1001) INVALID_NUMBER (sqlcode = -1722) LOGIN_DENIED (sqlcode = -1017) NO_DATA_FOUND (sqlcode = -1403) TOO_MANY_ROWS (sqlcode = -1422) …etc… These are named in the ‘standard’ package in pl/sql.

These exception names do not need to be declared. To handle them explicitly, put a clause in the exception section: EXCEPTION When DUP_VAL_ON_INDEX Then dbms_output.put_line(‘record already there’); END;

DECLARE v_student_id char(5) := &sv_student_id; v_first_name VARCHAR2(35); v_last_name VARCHAR2(35); BEGIN SELECT first, last INTO v_first_name, v_last_name FROM student WHERE studentid = v_student_id; DBMS_OUTPUT.PUT_LINE ('Student name: '||v_first_name||' '||v_last_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('There is no such student'); END;

So far, you have seen examples of the programs able to handle a single exception only. For example, a PL/SQL contains an exception- handler with a single exception ZERO_DIVIDE. However, many times in the PL/SQL block you need to handle different exceptions. Moreover, often you need to specify different actions that must be taken when a particular exception is raised.

DECLARE v_student_id NUMBER := &sv_student_id; v_enrolled VARCHAR2(3) := 'NO'; BEGIN DBMS_OUTPUT.PUT_LINE (‘Check if the student is enrolled’); SELECT ‘YES’ INTO v_enrolled FROM enrollment WHERE student_id = v_student_id; DBMS_OUTPUT.PUT_LINE (‘The student is enrolled into one course’); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE(‘The student is not enrolled’); WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE (‘The student is enrolled into many courses’); END; * This example contains two exceptions in the single exception handling section. * The first exception, NO_DATA_FOUND, will be raised if there are no records in the ENROLLMENT table for a particular student. * The second exception, TOO_MANY_ROWS, will be raised if a particular student is enrolled into more than one course.

You have seen examples of exception-handling sections that have particular exceptions, such as NO_DATA_FOUND or ZERO_DIVIDE. However, you cannot always predict beforehand what exception might be raised by your PL/SQL block. In cases like this, there is a special exception handler called OTHERS. All predefined Oracle errors (exceptions) can be handled with the help of the OTHERS handler.

Example DECLARE v_instructor_id NUMBER := &sv_instructor_id; v_instructor_name VARCHAR2(50); BEGIN SELECT first_name||' '||last_name INTO v_instructor_name FROM instructor WHERE instructor_id = v_instructor_id; DBMS_OUTPUT.PUT_LINE (‘Instructor name is’ ||v_instructor_name); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(‘An error has occurred’); END; When run, this example produces the following output: Enter value for sv_instructor_id: 100 old 2: v_instructor_id NUMBER := &sv_instructor_id; new 2: v_instructor_id NUMBER := 100; An error has occurred PL/SQL procedure successfully completed. This demonstrates not only the use of the OTHERS exception handler, but also a bad programming practice. The exception OTHERS has been raised because there is no record in the INSTRUCTOR table for instructor ID 100.

 These errors are not pre-named, but have a number.  They will be raised automatically by the RDBMS.  The EXCEPTION section handles them in the WHEN OTHERS clause.  To name an unnamed error in your application: Give the error a name using a PRAGMA, or compiler directive, called EXCEPTION_INIT. PL/SQL or RDBMS raise the error automatically. Handle the error in a specially written WHEN clause in the exception section.

 We’ve all seen errors that Oracle throws at us: * ERROR: ORA=12170: TNS: Connect timeout occurred * TNS Listener does not currently know of service requested in connect descriptor  Note: * All of these errors have an error number: * e.g. ORA = means that the connection timeout occurred.  These errors are RAISED automatically by the system, because they are system errors.

DECLARE deadlock_detected EXCEPTION; PRAGMA EXCEPTION_INIT(deadlock_detected, -60); BEGIN Some operation that causes an ORA error (see locking) EXCEPTION WHEN deadlock_detected THEN -- handle the error END;

This type of exception is called a user-defined exception because it is defined by the programmer. Before the exception can be used, it must be declared. A user-defined exception is declared in the declaration part of a PL/SQL block as shown below: DECLARE exception_name EXCEPTION; Once an exception has been declared, the executable statements associated with this exception are specified in the exception-handling section of the block. The format of the exception-handling section is the same as for named exceptions.

DECLARE e_invalid_id EXCEPTION; BEGIN … EXCEPTION WHEN e_invalid_id THEN DBMS_OUTPUT.PUT_LINE ('An id cannot be negative'); END;

A user-defined exception must be raised explicitly. In other words, you need to specify in your program under which circumstances an exception must be raised as shown : DECLARE exception_name EXCEPTION; BEGIN … IF CONDITION THEN RAISE exception_name; ELSE … END IF; EXCEPTION WHEN exception_name THEN ERROR-PROCESSING STATEMENTS; END;