Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Slides:



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

PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
ORACLE TRANSACTIONS A transaction begins with the first executable SQL statement after a commit, rollback or connection made to the Oracle engine. All.
Chapter 4B: More Advanced PL/SQL Programming
PL/SQL (Embedded SQL) Introduction Benefits Basic Constructs
PL/SQL Agenda: Basic PL/SQL block structure
LOCKS Locks are mechanisms used to ensure data integrity while allowing maximum concurrent access to data. Oracle's locking is fully automatic and requires.
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.
1 CONCURRENCY CONTROL IN ORACLE Users manipulate Oracle table data via SQL or PL/SQL sentences. An Oracle transaction can be made up of a single SQL. sentence.
1 Agenda Summary of last class Loops Simple Loops WHILE Loops FOR Loops Records Cursors.
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.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
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.
1 CursorsCursors. 2 SQL Cursor A cursor is a private SQL work area. A cursor is a private SQL work area. There are two types of cursors: There are two.
PL/SQL Procedural Language / Structured Query Language.
Oracle PL/SQL. PL/SQL Originally modeled after ADA Originally modeled after ADA Created for Dept. of DefenseCreated for Dept. of Defense Allows expanded.
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.
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.
© 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post.
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.
implicit and an explicit cursor
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
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.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
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.
Oracle 数据库应用 -- PL/SQL 进阶 (3) & Oracle DBA 2016/5/ /5/10.
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
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
Difference between Oracle PL/SQL and MySQL
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
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

Exception Handling in PL/SQL

POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions

What is Exception Handling Error condition in PL/SQL is called exception. A part of Executable section Between Begin… and end; Using Exception Handling we can test the code and avoid it from exiting abruptly. When an exception occurs a message which explains its cause is received.

Exception consists of 3 parts Type of Exception An Error Code A message By handling exceptions we ensure a PL/SQL block does not exit abruptly.

Syntax for exception DECLARE Exception Declaration BEGIN Executable statements… EXCEPTION WHEN ex_name1 THEN -Error handling statements WHEN ex_name2 THEN -Error handling statements WHEN Others THEN -Error handling statements END; When an exception is raised, Oracle searches for an appropriate exception handler in the exception section. Only one exception can be raised in a Block and the control does not return to the Execution Section after the error is handled.

Types of Exceptions Pre defined Exceptions Unnamed System Exceptions User-defined Exceptions

Named System Exceptions (Predefined) Automatically raised by Oracle when a program violates a RDBMS rule System exceptions that are raised frequently are pre- defined and given a name in Oracle Not Declared explicitly Raised implicitly when a predefined Oracle error occurs Caught by referencing the standard name within an exception-handling routine

Some Named (Predefined) Exceptions Exception NameReasonError Number CURSOR_ALREADY_OPEN When you open a cursor that is already open. ORA INVALID_CURSOR When you perform an invalid operation on a cursor like closing a cursor, fetch data from a cursor that is not opened. ORA NO_DATA_FOUND When a SELECT...INTO clause does not return any row from a table. ORA TOO_MANY_ROWS When you SELECT or fetch more than one row into a record or variable. ORA ZERO_DIVIDEWhen you attempt to divide a number by zero. ORA-01476

Predefined Exception Example declare v_empno emp.empno%type; v_ename emp.ename%type; v_sal emp.sal%type; a number; begin a:=&a; select empno,ename,sal into v_empno,v_ename,v_sal from emp where empno=&v_empno; v_sal:=v_sal+v_sal/a; dbms_output.put_line('Employee number is '||v_empno || ' and name is '||v_ename); dbms_output.put_line('New salary is '||v_sal); exception when no_data_found then dbms_output.put_line('NO SUCH RECORD!!!'); when zero_divide then dbms_output.put_line('Salary cannot be divided by zero'); when others then dbms_output.put_line('Some error.GOD knows what???'); end; /

Unnamed System Exceptions System exception for which oracle does not provide a name is known as unnamed system exception. These exceptions do not occur frequently. These Exceptions have a code and an associated message. There are two ways to handle unnamed system exceptions: 1. By using the WHEN OTHERS exception handler, or 2. By associating the exception code to a name and using it as a named exception.

Unnamed exceptions We can assign a name to unnamed system exceptions using Pragma EXCEPTION_INIT. EXCEPTION_INIT will associate a predefined Oracle error number to a programmer_defined exception name.

Syntax for unnamed system exception using EXCEPTION_INIT DECLARE exception_name EXCEPTION; PRAGMA EXCEPTION_INIT (exception_name, Err_code); BEGIN Execution section EXCEPTION WHEN exception_name THEN handle the exception END;

Example DECLARE dup_pr_key exception; pragma exception_init(dup_pr_key,-1); BEGIN insert into emp(empno,ename) values(1111,'ABCD'); dbms_output.put_line('One record successfully inserted'); insert into emp(empno,ename) values(1111,'EFGH'); dbms_output.put_line('One more record successfully inserted'); EXCEPTION when dup_pr_key then dbms_output.put_line('How come more employees with the same employee number !!!'); END; /

User-defined Exceptions They should be explicitly declared in the declaration section. They should be explicitly raised in the Execution Section. They should be handled by referencing the user- defined exception name in the exception section.

User defined Exception-Example declare v_empno emp.empno%type; v_ename emp.ename%type; v_sal emp.sal%type; a number; lo_sal exception; begin a:=&a; select empno,ename,sal into v_empno,v_ename,v_sal from emp where empno=&v_empno; v_sal:=v_sal+v_sal/a; if v_Sal<2000 then raise lo_sal; end if; dbms_output.put_line('Employee number is '||v_empno || ' and name is '||v_ename); dbms_output.put_line('New salary is '||v_sal); exception when lo_sal then dbms_output.put_line('In these times of Inflation INCREASE THE SALARY !!!'); when others then dbms_output.put_line('Some error.GOD knows what???'); end;

RAISE_APPLICATION_ERROR ( ) a built-in procedure in oracle used to display the user-defined error messages along with the error number range of error number between and Whenever a message is displayed using RAISE_APPLICATION_ERROR, all previous transactions which are not committed within the PL/SQL Block are rolled back automatically (i.e. change due to INSERT, UPDATE, or DELETE statements). raises an exception but does not handle it.

WHY RAISE_APPLICATION_ERROR ? to create a unique id for an user-defined exception. to make the user-defined exception look like an Oracle error.

Syntax RAISE_APPLICATION_ERROR (error_number, error_message); The Error number must be between and The Error message is the message you want to display when the error occurs.

Steps for RAISE_APPLICATION_ERROR procedure: Declare a user-defined exception in the declaration section. Raise the user-defined exception based on a specific business rule in the execution section. Finally, catch the exception and link the exception to a user-defined error number in RAISE_APPLICATION_ERROR.

RAISE_APPLICATION_ERROR -Example DECLARE v_empno emp.empno%type; v_ename emp.ename%type; v_sal emp.sal%type; a number; lo_sal exception; BEGIN a:=&a; select empno,ename,sal into v_empno,v_ename,v_sal from emp where empno=&v_empno; v_sal:=v_sal+v_sal/a; if v_Sal<2000 then raise lo_sal; end if; dbms_output.put_line('Employee number is '||v_empno || ' and name is '||v_ename); dbms_output.put_line('New salary is '||v_sal);

RAISE_APPLICATION_ERROR – Example(contd.) EXCEPTION when lo_sal then RAISE_APPLICATION_ERROR(-20001,'In these times of Inflation INCREASE THE SALARY !!!'); when others then dbms_output.put_line('Some error.GOD knows what???'); END ;

THANKS