SlideShare a Scribd company logo
Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
23
Handling Exceptions
23-2 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Objectives
After completing this lesson, you should
be able to do the following:
• Define PL/SQL exceptions
• Recognize unhandled exceptions
• List and use different types of PL/SQL
exception handlers
• Trap unanticipated errors
• Describe the effect of exception
propagation in nested blocks
• Customize PL/SQL exception messages
23-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Handling Exceptions with PL/SQL
• What is an exception?
Identifier in PL/SQL that is raised during
execution
• How is it raised?
– An Oracle error occurs.
– You raise it explicitly.
• How do you handle it?
– Trap it with a handler.
– Propagate it to the calling environment.
23-4 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Handling Exceptions
Trap the exception
DECLARE
BEGIN
END;
Exception
is raised
EXCEPTION
Exception
is trapped
Propagate the exception
DECLARE
BEGIN
END;
Exception
is raised
EXCEPTION
Exception is
not trapped
Exception
propagates to calling
environment
23-5 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Exception Types
• Predefined Oracle Server
• Non-predefined Oracle Server
• User-defined
} Implicitly
raised
Explicitly raised
23-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Trapping Exceptions
EXCEPTION
WHEN exception1 [OR exception2 . . .] THEN
statement1;
statement2;
. . .
[WHEN exception3 [OR exception4 . . .] THEN
statement1;
statement2;
. . .]
[WHEN OTHERS THEN
statement1;
statement2;
. . .]
Syntax
23-7 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Trapping Exceptions Guidelines
• WHEN OTHERS is the last clause.
• EXCEPTION keyword starts exception-
handling section.
• Several exception handlers are allowed.
• Only one handler is processed before
leaving the block.
23-8 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Trapping Predefined
Oracle Server Errors
• Reference the standard name in the
exception-handling routine.
• Sample predefined exceptions:
– NO_DATA_FOUND
– TOO_MANY_ROWS
– INVALID_CURSOR
– ZERO_DIVIDE
– DUP_VAL_ON_INDEX
23-10 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Predefined Exception
BEGIN
EXCEPTION
WHEN NO_DATA_FOUND THEN
statement1;
statement2;
WHEN TOO_MANY_ROWS THEN
statement1;
WHEN OTHERS THEN
statement1;
statement2;
statement3;
END;
Syntax
23-11 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Trapping Non-Predefined Oracle
Server Errors
Declare
• Name the
exception
Associate
• Code the PRAGMA
EXCEPTION_INIT
Declarative section
Reference
• Handle the
raised
exception
Exception-handling
section
23-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
DECLARE
e_emps_remaining EXCEPTION;
PRAGMA EXCEPTION_INIT (
e_emps_remaining, -2292);
v_deptno dept.deptno%TYPE := &p_deptno;
BEGIN
DELETE FROM dept
WHERE deptno = v_deptno;
COMMIT;
EXCEPTION
WHEN e_emps_remaining THEN
DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' ||
TO_CHAR(v_deptno) || '. Employees exist. ');
END;
Non-Predefined Error
Trap for Oracle Server error number
–2292, an integrity constraint violation.
e_emps_remaining EXCEPTION; 1
PRAGMA EXCEPTION_INIT (
e_emps_remaining, -2292); 2
e_emps_remaining 3
23-13 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Functions for Trapping
Exceptions
• SQLCODE
Returns the numeric value for the error
code
• SQLERRM
Returns the message associated with the
error number
23-14 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Functions for Trapping Exceptions
DECLARE
v_error_code NUMBER;
v_error_message VARCHAR2(255);
BEGIN
...
EXCEPTION
...
WHEN OTHERS THEN
ROLLBACK;
v_error_code := SQLCODE ;
v_error_message := SQLERRM ;
INSERT INTO errors
VALUES(v_error_code, v_error_message);
END;
Example
SQLCODE
SQLERRM
23-15 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Trapping User-Defined
Exceptions
• Name the
exception
Declare
Declarative
section
Raise
• Explicitly raise
the exception by
using the RAISE
statement
Executable
section
Reference
• Handle the
raised
exception
Exception-handling
section
23-16 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
User-Defined Exception
DECLARE
e_invalid_product EXCEPTION;
BEGIN
UPDATE product
SET descrip = '&product_description'
WHERE prodid = &product_number;
IF SQL%NOTFOUND THEN
RAISE e_invalid_product;
END IF;
COMMIT;
EXCEPTION
WHEN e_invalid_product THEN
DBMS_OUTPUT.PUT_LINE('Invalid product number.');
END;
Example
e_invalid_product EXCEPTION; 1
RAISE e_invalid_product; 2
e_invalid_product 3
23-17 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Calling Environments
SQL*Plus
Procedure
Builder
Oracle
Developer
Forms
Precompiler
application
An enclosing
PL/SQL block
Displays error number and message
to screen
Displays error number and message
to screen
Accesses error number and message
in a trigger by means of the
ERROR_CODE and ERROR_TEXT
packaged functions
Accesses exception number through
the SQLCA data structure
Traps exception in exception-
handling routine of enclosing block
23-18 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Propagating Exceptions
BEGIN
SELECT ...
UPDATE ...
IF SQL%NOTFOUND THEN
RAISE e_no_rows;
END IF;
EXCEPTION
WHEN e_integrity THEN ...
WHEN e_no_rows THEN ...
END;
DECLARE
. . .
e_no_rows exception;
e_integrity exception;
PRAGMA EXCEPTION_INIT (e_integrity, -2292);
BEGIN
FOR c_record IN emp_cursor LOOP
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN . . .
WHEN TOO_MANY_ROWS THEN . . .
END;
Subblocks can handle
an exception or pass
the exception to the
enclosing block.
BEGIN
SELECT ...
UPDATE ...
IF SQL%NOTFOUND THEN
RAISE e_no_rows;
END IF;
EXCEPTION
WHEN e_integrity THEN ...
WHEN e_no_rows THEN ...
END;
23-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
RAISE_APPLICATION_ERROR
Procedure
Syntax
• A procedure that lets you issue user-
defined error messages from stored
subprograms
• Called only from an executing stored
subprogram
raise_application_error (error_number,
message[, {TRUE | FALSE}]);
23-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
RAISE_APPLICATION_ERROR
Procedure
• Used in two different places:
– Executable section
– Exception section
• Returns error conditions to the user in a
manner consistent with other Oracle
Server errors
23-21 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Summary
• Exception types:
– Predefined Oracle Server error
– Non-predefined Oracle Server error
– User-defined error
• Exception trapping
• Exception handling:
– Trap the exception within the PL/SQL
block.
– Propagate the exception.
23-22 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Practice Overview
• Handling named exceptions
• Creating and invoking user-defined
exceptions

More Related Content

What's hot

Les01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL StatementsLes01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL Statements
siavosh kaviani
 
Les07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column SubqueriesLes07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column Subqueries
siavosh kaviani
 
Les11[1]Including Constraints
Les11[1]Including ConstraintsLes11[1]Including Constraints
Les11[1]Including Constraints
siavosh kaviani
 
Les06[1]Subqueries
Les06[1]SubqueriesLes06[1]Subqueries
Les06[1]Subqueries
siavosh kaviani
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objects
siavosh kaviani
 
Les20[1]Working with Composite Datatypes
Les20[1]Working with Composite DatatypesLes20[1]Working with Composite Datatypes
Les20[1]Working with Composite Datatypes
siavosh kaviani
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
siavosh kaviani
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tables
siavosh kaviani
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
siavosh kaviani
 
Les05[1]Aggregating Data Using Group Functions
Les05[1]Aggregating Data  Using Group FunctionsLes05[1]Aggregating Data  Using Group Functions
Les05[1]Aggregating Data Using Group Functions
siavosh kaviani
 
Les03[1] Single-Row Functions
Les03[1] Single-Row FunctionsLes03[1] Single-Row Functions
Les03[1] Single-Row Functions
siavosh kaviani
 
11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar
rehaniltifat
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
rehaniltifat
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development
rehaniltifat
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
mohdoracle
 
06 Using More Package Concepts
06 Using More Package Concepts06 Using More Package Concepts
06 Using More Package Concepts
rehaniltifat
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
Nick Buytaert
 

What's hot (20)

Les01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL StatementsLes01[1]Writing Basic SQL Statements
Les01[1]Writing Basic SQL Statements
 
Les07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column SubqueriesLes07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column Subqueries
 
Les11[1]Including Constraints
Les11[1]Including ConstraintsLes11[1]Including Constraints
Les11[1]Including Constraints
 
Les06[1]Subqueries
Les06[1]SubqueriesLes06[1]Subqueries
Les06[1]Subqueries
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objects
 
Les20[1]Working with Composite Datatypes
Les20[1]Working with Composite DatatypesLes20[1]Working with Composite Datatypes
Les20[1]Working with Composite Datatypes
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tables
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 
Les05[1]Aggregating Data Using Group Functions
Les05[1]Aggregating Data  Using Group FunctionsLes05[1]Aggregating Data  Using Group Functions
Les05[1]Aggregating Data Using Group Functions
 
Les03[1] Single-Row Functions
Les03[1] Single-Row FunctionsLes03[1] Single-Row Functions
Les03[1] Single-Row Functions
 
Plsql
PlsqlPlsql
Plsql
 
11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development
 
plsql Lec11
plsql Lec11 plsql Lec11
plsql Lec11
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
plsql Les07
plsql Les07 plsql Les07
plsql Les07
 
06 Using More Package Concepts
06 Using More Package Concepts06 Using More Package Concepts
06 Using More Package Concepts
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 

Similar to Les23[1]Handling Exceptions

Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08
Thuan Nguyen
 
04 Handling Exceptions
04 Handling Exceptions04 Handling Exceptions
04 Handling Exceptions
rehaniltifat
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handling
Smitha Padmanabhan
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
Quang Minh Đoàn
 
Error management
Error managementError management
Error managementdaniil3
 
Plsql guide 2
Plsql guide 2Plsql guide 2
Plsql guide 2
Vinay Kumar
 
Exception Handling
Exception Handling Exception Handling
Exception Handling
Swapnali Pawar
 
Pl sql chapter 1
Pl sql chapter 1Pl sql chapter 1
Pl sql chapter 1
PrabhatKumar591
 
Exceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant BhabadExceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant Bhabad
vasant Bhabad
 
A exception ekon16
A exception ekon16A exception ekon16
A exception ekon16
Max Kleiner
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programmingRushdi Shams
 
SQL / PL
SQL / PLSQL / PL
SQL / PL
srijanani2030
 
Bypass dbms assert
Bypass dbms assertBypass dbms assert
Bypass dbms assertfangjiafu
 
PL/SQL Interview Questions
PL/SQL Interview QuestionsPL/SQL Interview Questions
PL/SQL Interview Questions
Srinimf-Slides
 
Pl sql-ch1
Pl sql-ch1Pl sql-ch1
Pl sql-ch1
Mukesh Tekwani
 
Exception handling and logging best practices
Exception handling and logging best practicesException handling and logging best practices
Exception handling and logging best practicesAngelin R
 
Oracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online TrainingOracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online Training
suresh
 
Take Full Advantage of the Oracle PL/SQL Compiler
Take Full Advantage of the Oracle PL/SQL CompilerTake Full Advantage of the Oracle PL/SQL Compiler
Take Full Advantage of the Oracle PL/SQL Compiler
Steven Feuerstein
 
7. exceptions handling in pl
7. exceptions handling in pl7. exceptions handling in pl
7. exceptions handling in pl
Amrit Kaur
 

Similar to Les23[1]Handling Exceptions (20)

Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08
 
04 Handling Exceptions
04 Handling Exceptions04 Handling Exceptions
04 Handling Exceptions
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handling
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
 
Error management
Error managementError management
Error management
 
Plsql guide 2
Plsql guide 2Plsql guide 2
Plsql guide 2
 
Exception Handling
Exception Handling Exception Handling
Exception Handling
 
Pl sql chapter 1
Pl sql chapter 1Pl sql chapter 1
Pl sql chapter 1
 
Exceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant BhabadExceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant Bhabad
 
A exception ekon16
A exception ekon16A exception ekon16
A exception ekon16
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 
SQL / PL
SQL / PLSQL / PL
SQL / PL
 
Bypass dbms assert
Bypass dbms assertBypass dbms assert
Bypass dbms assert
 
PL/SQL Interview Questions
PL/SQL Interview QuestionsPL/SQL Interview Questions
PL/SQL Interview Questions
 
Pl sql-ch1
Pl sql-ch1Pl sql-ch1
Pl sql-ch1
 
Exception handling and logging best practices
Exception handling and logging best practicesException handling and logging best practices
Exception handling and logging best practices
 
Oracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online TrainingOracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online Training
 
Pl sql
Pl sqlPl sql
Pl sql
 
Take Full Advantage of the Oracle PL/SQL Compiler
Take Full Advantage of the Oracle PL/SQL CompilerTake Full Advantage of the Oracle PL/SQL Compiler
Take Full Advantage of the Oracle PL/SQL Compiler
 
7. exceptions handling in pl
7. exceptions handling in pl7. exceptions handling in pl
7. exceptions handling in pl
 

More from siavosh kaviani

sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Applicationsara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
siavosh kaviani
 
Introduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdfIntroduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdf
siavosh kaviani
 
Attaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptxAttaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptx
siavosh kaviani
 
Short CV BA.pdf
Short CV BA.pdfShort CV BA.pdf
Short CV BA.pdf
siavosh kaviani
 
Faegh Omidi Resume.pdf
Faegh Omidi Resume.pdfFaegh Omidi Resume.pdf
Faegh Omidi Resume.pdf
siavosh kaviani
 
Short CV CTO version 2.pdf
Short CV CTO version 2.pdfShort CV CTO version 2.pdf
Short CV CTO version 2.pdf
siavosh kaviani
 
Short CV Marketing version 2.pdf
Short CV Marketing version 2.pdfShort CV Marketing version 2.pdf
Short CV Marketing version 2.pdf
siavosh kaviani
 
Short CV prof version 2.pdf
Short CV prof version 2.pdfShort CV prof version 2.pdf
Short CV prof version 2.pdf
siavosh kaviani
 
Siavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdfSiavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdf
siavosh kaviani
 
SiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdfSiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdf
siavosh kaviani
 
apex security demo.ppsx
apex security demo.ppsxapex security demo.ppsx
apex security demo.ppsx
siavosh kaviani
 
Les14[1]Controlling User Access
Les14[1]Controlling User AccessLes14[1]Controlling User Access
Les14[1]Controlling User Access
siavosh kaviani
 
Les15[1]SQL Workshop
Les15[1]SQL WorkshopLes15[1]SQL Workshop
Les15[1]SQL Workshop
siavosh kaviani
 

More from siavosh kaviani (13)

sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Applicationsara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
 
Introduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdfIntroduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdf
 
Attaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptxAttaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptx
 
Short CV BA.pdf
Short CV BA.pdfShort CV BA.pdf
Short CV BA.pdf
 
Faegh Omidi Resume.pdf
Faegh Omidi Resume.pdfFaegh Omidi Resume.pdf
Faegh Omidi Resume.pdf
 
Short CV CTO version 2.pdf
Short CV CTO version 2.pdfShort CV CTO version 2.pdf
Short CV CTO version 2.pdf
 
Short CV Marketing version 2.pdf
Short CV Marketing version 2.pdfShort CV Marketing version 2.pdf
Short CV Marketing version 2.pdf
 
Short CV prof version 2.pdf
Short CV prof version 2.pdfShort CV prof version 2.pdf
Short CV prof version 2.pdf
 
Siavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdfSiavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdf
 
SiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdfSiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdf
 
apex security demo.ppsx
apex security demo.ppsxapex security demo.ppsx
apex security demo.ppsx
 
Les14[1]Controlling User Access
Les14[1]Controlling User AccessLes14[1]Controlling User Access
Les14[1]Controlling User Access
 
Les15[1]SQL Workshop
Les15[1]SQL WorkshopLes15[1]SQL Workshop
Les15[1]SQL Workshop
 

Recently uploaded

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 

Recently uploaded (20)

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 

Les23[1]Handling Exceptions

  • 1. Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. 23 Handling Exceptions
  • 2. 23-2 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • Define PL/SQL exceptions • Recognize unhandled exceptions • List and use different types of PL/SQL exception handlers • Trap unanticipated errors • Describe the effect of exception propagation in nested blocks • Customize PL/SQL exception messages
  • 3. 23-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Handling Exceptions with PL/SQL • What is an exception? Identifier in PL/SQL that is raised during execution • How is it raised? – An Oracle error occurs. – You raise it explicitly. • How do you handle it? – Trap it with a handler. – Propagate it to the calling environment.
  • 4. 23-4 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Handling Exceptions Trap the exception DECLARE BEGIN END; Exception is raised EXCEPTION Exception is trapped Propagate the exception DECLARE BEGIN END; Exception is raised EXCEPTION Exception is not trapped Exception propagates to calling environment
  • 5. 23-5 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Exception Types • Predefined Oracle Server • Non-predefined Oracle Server • User-defined } Implicitly raised Explicitly raised
  • 6. 23-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Trapping Exceptions EXCEPTION WHEN exception1 [OR exception2 . . .] THEN statement1; statement2; . . . [WHEN exception3 [OR exception4 . . .] THEN statement1; statement2; . . .] [WHEN OTHERS THEN statement1; statement2; . . .] Syntax
  • 7. 23-7 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Trapping Exceptions Guidelines • WHEN OTHERS is the last clause. • EXCEPTION keyword starts exception- handling section. • Several exception handlers are allowed. • Only one handler is processed before leaving the block.
  • 8. 23-8 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Trapping Predefined Oracle Server Errors • Reference the standard name in the exception-handling routine. • Sample predefined exceptions: – NO_DATA_FOUND – TOO_MANY_ROWS – INVALID_CURSOR – ZERO_DIVIDE – DUP_VAL_ON_INDEX
  • 9. 23-10 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Predefined Exception BEGIN EXCEPTION WHEN NO_DATA_FOUND THEN statement1; statement2; WHEN TOO_MANY_ROWS THEN statement1; WHEN OTHERS THEN statement1; statement2; statement3; END; Syntax
  • 10. 23-11 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Trapping Non-Predefined Oracle Server Errors Declare • Name the exception Associate • Code the PRAGMA EXCEPTION_INIT Declarative section Reference • Handle the raised exception Exception-handling section
  • 11. 23-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. DECLARE e_emps_remaining EXCEPTION; PRAGMA EXCEPTION_INIT ( e_emps_remaining, -2292); v_deptno dept.deptno%TYPE := &p_deptno; BEGIN DELETE FROM dept WHERE deptno = v_deptno; COMMIT; EXCEPTION WHEN e_emps_remaining THEN DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' || TO_CHAR(v_deptno) || '. Employees exist. '); END; Non-Predefined Error Trap for Oracle Server error number –2292, an integrity constraint violation. e_emps_remaining EXCEPTION; 1 PRAGMA EXCEPTION_INIT ( e_emps_remaining, -2292); 2 e_emps_remaining 3
  • 12. 23-13 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Functions for Trapping Exceptions • SQLCODE Returns the numeric value for the error code • SQLERRM Returns the message associated with the error number
  • 13. 23-14 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Functions for Trapping Exceptions DECLARE v_error_code NUMBER; v_error_message VARCHAR2(255); BEGIN ... EXCEPTION ... WHEN OTHERS THEN ROLLBACK; v_error_code := SQLCODE ; v_error_message := SQLERRM ; INSERT INTO errors VALUES(v_error_code, v_error_message); END; Example SQLCODE SQLERRM
  • 14. 23-15 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Trapping User-Defined Exceptions • Name the exception Declare Declarative section Raise • Explicitly raise the exception by using the RAISE statement Executable section Reference • Handle the raised exception Exception-handling section
  • 15. 23-16 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. User-Defined Exception DECLARE e_invalid_product EXCEPTION; BEGIN UPDATE product SET descrip = '&product_description' WHERE prodid = &product_number; IF SQL%NOTFOUND THEN RAISE e_invalid_product; END IF; COMMIT; EXCEPTION WHEN e_invalid_product THEN DBMS_OUTPUT.PUT_LINE('Invalid product number.'); END; Example e_invalid_product EXCEPTION; 1 RAISE e_invalid_product; 2 e_invalid_product 3
  • 16. 23-17 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Calling Environments SQL*Plus Procedure Builder Oracle Developer Forms Precompiler application An enclosing PL/SQL block Displays error number and message to screen Displays error number and message to screen Accesses error number and message in a trigger by means of the ERROR_CODE and ERROR_TEXT packaged functions Accesses exception number through the SQLCA data structure Traps exception in exception- handling routine of enclosing block
  • 17. 23-18 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Propagating Exceptions BEGIN SELECT ... UPDATE ... IF SQL%NOTFOUND THEN RAISE e_no_rows; END IF; EXCEPTION WHEN e_integrity THEN ... WHEN e_no_rows THEN ... END; DECLARE . . . e_no_rows exception; e_integrity exception; PRAGMA EXCEPTION_INIT (e_integrity, -2292); BEGIN FOR c_record IN emp_cursor LOOP END LOOP; EXCEPTION WHEN NO_DATA_FOUND THEN . . . WHEN TOO_MANY_ROWS THEN . . . END; Subblocks can handle an exception or pass the exception to the enclosing block. BEGIN SELECT ... UPDATE ... IF SQL%NOTFOUND THEN RAISE e_no_rows; END IF; EXCEPTION WHEN e_integrity THEN ... WHEN e_no_rows THEN ... END;
  • 18. 23-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. RAISE_APPLICATION_ERROR Procedure Syntax • A procedure that lets you issue user- defined error messages from stored subprograms • Called only from an executing stored subprogram raise_application_error (error_number, message[, {TRUE | FALSE}]);
  • 19. 23-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. RAISE_APPLICATION_ERROR Procedure • Used in two different places: – Executable section – Exception section • Returns error conditions to the user in a manner consistent with other Oracle Server errors
  • 20. 23-21 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Summary • Exception types: – Predefined Oracle Server error – Non-predefined Oracle Server error – User-defined error • Exception trapping • Exception handling: – Trap the exception within the PL/SQL block. – Propagate the exception.
  • 21. 23-22 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Practice Overview • Handling named exceptions • Creating and invoking user-defined exceptions