Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.

Slides:



Advertisements
Similar presentations
Chapter 4B: More Advanced PL/SQL Programming
Advertisements

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.
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 ;
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.
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.
1 Introduction to PL/SQL. 2  Procedural programming language  Uses detailed instructions  Processes statements sequentially  Combines SQL commands.
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:
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.
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.
I Copyright © 2004, Oracle. All rights reserved. Introduction Copyright © 2004, Oracle. All rights reserved.
11 Copyright س Oracle Corporation, All rights reserved. ® Overview of PL/SQL.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
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.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Dynamic SQL Oracle Database PL/SQL 10g Programming Chapter 13.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
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.
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 
Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
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.
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.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
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.
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.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
ITEC 224 Database Programming
CHAPTER 5 EXCEPTION HANDLING
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
DataBase Logic in Business Applications
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.
PL/SQL Scripting in Oracle:
Handling Exceptions.
Handling Exceptions.
Database Management Systems 2
Handling Exceptions.
PRACTICE OVERVIEW PL/SQL Part - 1.
Database Programming Using Oracle 11g
Presentation transcript:

Exceptions Oracle Database PL/SQL 10g Programming Chapter 7

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 2 Exceptions Exception Handling Exception Handling Declaring Exceptions Declaring Exceptions Raising Exceptions Raising Exceptions Handling Exceptions Handling Exceptions Error Stack Management Error Stack Management

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 3 Exceptions Exception Handling: Definition Exceptions are failures in a programs compilation or execution. Exceptions are failures in a programs compilation or execution. Exceptions can be critical or non-critical failures, the former should stop execution of the program and undo changes while the latter may be recorded and examined later. Exceptions can be critical or non-critical failures, the former should stop execution of the program and undo changes while the latter may be recorded and examined later.

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 4 Exceptions Exception Handling: Error Types Compile-time Errors Compile-time Errors Raised errors due to syntax mistakes Raised errors due to syntax mistakes Raised errors due to reserved word use Raised errors due to reserved word use Run-time Errors Run-time Errors Raised by logical programming mistakes Raised by logical programming mistakes Raised by unexpected data states Raised by unexpected data states

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 5 Exceptions Declaring Exceptions: Types Predefined Exceptions Predefined Exceptions Standard event exceptions are defined, like: Standard event exceptions are defined, like: NO_DATA_FOUND NO_DATA_FOUND NOT_LOGGED_ON NOT_LOGGED_ON TOO_MANY_ROWS TOO_MANY_ROWS Package event exceptions are defined, like: Package event exceptions are defined, like: INVALID_DIRECTORY (DBMS_LOB package) INVALID_DIRECTORY (DBMS_LOB package) INCONSISTENT_TYPE (DBMS_SQL package) INCONSISTENT_TYPE (DBMS_SQL package) User-defined Exceptions User-defined Exceptions Are defined in anonymous or named blocks Are defined in anonymous or named blocks Are best placed in package specifications Are best placed in package specifications

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 6 Exceptions Declaring Exceptions: Predefined Predefined Exceptions Predefined Exceptions Have an assigned Oracle error number. Have an assigned Oracle error number. ORA-0001 ORA-0001 Have an assigned Oracle exception name. Have an assigned Oracle exception name. DUP_VAL_ON_INDEX DUP_VAL_ON_INDEX Have an assigned Oracle description. Have an assigned Oracle description. Unique constraint violated. Unique constraint violated.

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 7 Exceptions Declaring Exceptions: Syntax DECLARE user_defined_error EXCEPTION; user_defined_error EXCEPTION;BEGIN … shown_in_later_example … … shown_in_later_example …END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 8 Exceptions Declaring Exceptions: Mapping Predefined Mapping Predefined Exceptions Mapping Predefined Exceptions Declare an EXCEPTION variable. Declare an EXCEPTION variable. Use the PRAGMA (precompiler instruction) EXCEPTION_INIT to map a user-defined EXCEPTION to a predefined error number. Use the PRAGMA (precompiler instruction) EXCEPTION_INIT to map a user-defined EXCEPTION to a predefined error number. Can increase readability of programs. Can increase readability of programs.

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 9 Exceptions Declaring Exceptions: Mapping Syntax DECLARE user_defined_error EXCEPTION; user_defined_error EXCEPTION; PRAGMA EXCEPTION_INIT(user_defined_error,-1400); PRAGMA EXCEPTION_INIT(user_defined_error,-1400);BEGIN -- Assuming NOT NULL constraints this raises an error. -- Assuming NOT NULL constraints this raises an error. INSERT INTO a_table (id, name) VALUES (NULL,NULL); INSERT INTO a_table (id, name) VALUES (NULL,NULL);EXCEPTION WHEN user_defined_error THEN WHEN user_defined_error THEN INSERT INTO a_log_table VALUES INSERT INTO a_log_table VALUES ('ORA against A_TABLE'); ('ORA against A_TABLE');END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 10 Exceptions Raising Exceptions: Types Raising a user-defined exception: Raising a user-defined exception: Declare a variable as an EXCEPTION data type in the declaration section. Declare a variable as an EXCEPTION data type in the declaration section. Call the RAISE command and variable name in the execution section. Call the RAISE command and variable name in the execution section. User-defined exceptions are called explicitly as opposed to predefined exceptions that are raised implicitly. User-defined exceptions are called explicitly as opposed to predefined exceptions that are raised implicitly. Raising a user-defined application exception: Raising a user-defined application exception: Declare and raise simultaneously an error in the execution section by using the: Declare and raise simultaneously an error in the execution section by using the:RAISE_APPLICATION_EXCEPTION()

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 11 Exceptions Raising Exceptions: Simple Syntax DECLARE user_defined_error EXCEPTION; user_defined_error EXCEPTION;BEGIN IF condition_1 <> condition_2 THEN IF condition_1 <> condition_2 THEN RAISE user_defined_error; RAISE user_defined_error; END IF; END IF;EXCEPTION … shown_in_later_example … … shown_in_later_example …END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 12 Exceptions Raising Exceptions: Application Syntax BEGIN IF 1 <> 2 THEN IF 1 <> 2 THEN RAISE_APPLICATION_ERROR(-20001,'user message'); RAISE_APPLICATION_ERROR(-20001,'user message'); END IF; END IF;EXCEPTION … shown_in_later_example … … shown_in_later_example …END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 13 Exceptions Handling Exceptions: Definition The EXCEPTION section handles exceptions. The EXCEPTION section handles exceptions. The WHEN error_name or OTHERS captures thrown exceptions. The WHEN error_name or OTHERS captures thrown exceptions. Further handling can occur inside the WHEN block, like validating an error number to branch handling procedures. Further handling can occur inside the WHEN block, like validating an error number to branch handling procedures. The RETURN command can be used after handling the exception to return the line below where the error occurred. The RETURN command can be used after handling the exception to return the line below where the error occurred. Unhandled exceptions will raise the thrown exception to the calling program. Unhandled exceptions will raise the thrown exception to the calling program.

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 14 Exceptions Handling Exceptions: Simple Syntax DECLARE user_defined_error EXCEPTION; user_defined_error EXCEPTION;BEGIN IF condition_1 <> condition_2 THEN IF condition_1 <> condition_2 THEN RAISE user_defined_error; RAISE user_defined_error; END IF; END IF;EXCEPTION WHEN user_defined_error THEN WHEN user_defined_error THEN INSERT INTO a_log_table VALUES INSERT INTO a_log_table VALUES ('ORA against A_TABLE'); ('ORA against A_TABLE');END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 15 Exceptions Handling Exceptions: Application Syntax BEGIN IF 1 <> 2 THEN IF 1 <> 2 THEN RAISE_APPLICATION_ERROR(-20001,'user message'); RAISE_APPLICATION_ERROR(-20001,'user message'); END IF; END IF;EXCEPTION WHEN OTHERS THEN WHEN OTHERS THEN IF SQLCODE = THEN IF SQLCODE = THEN dbms_output.put_line('Expected.'); dbms_output.put_line('Expected.'); RETURN; RETURN; ELSE ELSE RAISE_APPLICATION_ERROR(-20002,'Unanticipated!'); RAISE_APPLICATION_ERROR(-20002,'Unanticipated!'); END IF; END IF;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 16 Exceptions Error Stack Management: Definition The error stack is the sequencing of errors from the triggering event to the calling block of code, which can be a SQL statement in SQL*Plus. The error stack is the sequencing of errors from the triggering event to the calling block of code, which can be a SQL statement in SQL*Plus. The DBMS_UTILITY package now has a FORMAT_ERROR_BACKTRACE procedure that enables you to capture the complete error stack. The DBMS_UTILITY package now has a FORMAT_ERROR_BACKTRACE procedure that enables you to capture the complete error stack.

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 17 Exceptions Error Stack Management: Stack Dependency

2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 18 Summary Exception Handling Exception Handling Declaring Exceptions Declaring Exceptions Raising Exceptions Raising Exceptions Handling Exceptions Handling Exceptions Error Stack Management Error Stack Management