SlideShare a Scribd company logo
1 of 18
Pl/sql exception
Errors
• Two types of errors can be found in a program:
compilation errors and runtime errors.
• There is a special section in a PL/SQL block that
handles the runtime errors.
• This section is called the exception-handling section,
and in it, runtime errors are referred to as
exceptions.
• The exception-handling section allows programmers
to specify what actions should be taken when a
specific exception occurs.
How to handle Exception
• In order to handle run time errors in the
program, an exception handler must be
added.
• The exception-handling section has the
following structure:
EXCEPTION
WHEN EXCEPTION_NAME
THEN
ERROR-PROCESSING STATEMENTS;
• The exception-handling section is placed
after the executable section of the block.
How exception can be classified???
 Build-in exception
 Userdefined exception
BUILT-IN EXCEPTIONS
 When a built-in exception occurs, it is said to
be raised implicitly.
 In other words, if a program breaks an Oracle
rule, the control is passed to the exception-
handling section of the block.
 At this point, the error processing statements
are executed.
 It is important for you to realize that after the
exception-handling section of the block has
executed, the block terminates.
 Control will not return to the executable section
of this block.
This example produces the
following output:
There is no such student
PL/SQL procedure
successfully completed.
Why???
Because there is no record
in the STUDENT table with
student ID 101, the SELECT
INTO statement does not
return any rows.
As a result, control passes
to the exception-handling
section of the block, and
the error message “There is
no such student” is
displayed on the screen.
Even though there is a
DBMS_OUTPUT.PUT_LINE
statement right after the
SELECT statement, it will
not be executed because
control has been
transferred to the
exception-handling section.
Example
DECLARE
v_student_name
VARCHAR2(50);
BEGIN
SELECT first_name||‘
’||last_name
INTO v_student_name
FROM student
WHERE student_id = 101;
DBMS_OUTPUT.PUT_LINE
(‘Student name
is’||v_student_name);
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.PUT_LINE
(‘There is no such student’);
END;
What are predefined exception??
 The list shown below explains some commonly used predefined
exceptions and how they are raised:
 NO_DATA_FOUND This exception is raised when a SELECT
INTO statement, which makes no calls to group functions, such
as SUM or COUNT, does not return any rows.
 For example, you issue a SELECT INTO statement against
STUDENT table where student ID equals 101.
 If there is no record in the STUDENT table passing this criteria
(student ID equals 101), the NO_DATA_FOUND exception is
raised.
TOO_MANY_ROWS
 This exception is raised when a SELECT INTO
statement returns more than one row.
 By definition, a SELECT INTO can return only single
row.
 If a SELECT INTO statement returns more than one
row, the definition of the SELECT INTO statement
is violated.
 This causes the TOO_MANY_ROWS exception to be
raised.
 For example, you issue a SELECT INTO statement
against the STUDENT table for a specific zip code.
 There is a big chance that this SELECT statement
will return more than one row because many
students can live in the same zip code area.
ZERO_DIVIDE
 This exception is raised when a division operation is
performed in the program and a divisor is equal to zero.
 Previous example in the illustrates how this exception is
raised.
LOGIN_DENIED
 This exception is raised when a user is trying to login on
to Oracle with invalid username or password.
PROGRAM_ERROR
 This exception is raised when a PL/SQL program
has an internal problem
VALUE_ERROR
 This exception is raised when conversion or size
mismatch error occurs.
 For example, you select student’s last name into a
variable that has been defined as VARCHAR2(5).
 If student’s last name contains more than five
characters, VALUE_ERROR exception is raised.
DUP_VALUE_ON_INDEX
 This exception is raised when a program tries to
store a duplicate value in the column or columns
that have a unique index defined on them.
 For example, you are trying to insert a record into
the SECTION table for the course number “25,”
section 1.
 If a record for the given course and section
numbers already exists in the SECTION table,
DUP_VAL_ON_INDEX exception is raised because
these columns have a unique index defined on
them.
Example
FROM enrollment
WHERE student_id = v_student_id;
DBMS_OUTPUT.PUT_LINE
('Student is registered for '||v_total||' course(s)');
*/inner block*/EXCEPTION
WHEN VALUE_ERROR OR INVALID_NUMBER
THEN
DBMS_OUTPUT.PUT_LINE('An error has
occurred');
END;
*/outer block */
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.PUT_LINE('There is no such student');
END;
• The inner block has structure similar to the outer block.
• It has a SELECT INTO statement and an exception
section to handle errors.
• When VALUE_ERROR or INVALID_NUMBER error occurs
in the inner block, the exception is raised.
• It is important that you realize that exceptions
VALUE_ERROR and INVALID_NUMBER have been
defined for the inner block only.
• Therefore, they can be raised in the inner block only.
• If one of these errors occurs in the outer block, this
program will be unable to terminate successfully.
User Defined Exceptions
• Often in programs you may need to handle
problems that are specific to the program you
write.
• For example, your program asks a user to enter
a value for student_id. This value is then
assigned to the variable v_student_id that is
used later in the program.
• Generally, you want a positive number for an id.
By mistake, the user enters a negative number.
• However, no error has occurred because
student_id has been defined as a number, and
the user has supplied a legitimate numeric
value.
• Therefore, you may want to implement your
own exception to handle this situation.
How to declare user-defined exception
• A user-defined exception is declared in
the declarative 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 built-in
exceptions
Raising Exception
• 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;
example
declare
e exception;
vamt number(10);
begin
select amt into vamt from
ac_det where acno=124;
vamt:=vamt-77000;
if(vamt<2000) then
raise e;
end if;
exception
when e then
dbms_output.put_line('Minimum
balance amount conflict');
* end;
SQL> /
Declare exception if the particular data
is not found
Raise exception ‘E’
If ‘E’ is exception the it will be
displayed as
‘minimum amount conflict’
Thank you
By R.sujaritha

More Related Content

What's hot (20)

Chapter8 pl sql
Chapter8 pl sqlChapter8 pl sql
Chapter8 pl sql
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
OOP java
OOP javaOOP java
OOP java
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
4. plsql
4. plsql4. plsql
4. plsql
 
Anomalies in database
Anomalies in databaseAnomalies in database
Anomalies in database
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
trigger dbms
trigger dbmstrigger dbms
trigger dbms
 
Packages in java
Packages in javaPackages in java
Packages in java
 
PLSQL Cursors
PLSQL CursorsPLSQL Cursors
PLSQL Cursors
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
04 Handling Exceptions
04 Handling Exceptions04 Handling Exceptions
04 Handling Exceptions
 
class and objects
class and objectsclass and objects
class and objects
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
 

Similar to PL/SQL EXCEPTION HANDLING

Introduction to PLSQL.PPT
Introduction to PLSQL.PPTIntroduction to PLSQL.PPT
Introduction to PLSQL.PPTSujayaBiju
 
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 08Thuan Nguyen
 
a basic java programming and data type.ppt
a basic java programming and data type.ppta basic java programming and data type.ppt
a basic java programming and data type.pptGevitaChinnaiah
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsmaxpane
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-onlyAshwin Kumar
 
Exception
ExceptionException
Exceptionwork
 
Exception
ExceptionException
Exceptionwork
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptxshivanka2
 
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docxCharles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docxchristinemaritza
 
London SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error HandlingLondon SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error HandlingRichard Clark
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management Systemsweetysweety8
 
Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6comp274
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6solutionjug4
 

Similar to PL/SQL EXCEPTION HANDLING (20)

Introduction to PLSQL.PPT
Introduction to PLSQL.PPTIntroduction to PLSQL.PPT
Introduction to PLSQL.PPT
 
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
 
Plsql guide 2
Plsql guide 2Plsql guide 2
Plsql guide 2
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
Pl sql
Pl sqlPl sql
Pl sql
 
a basic java programming and data type.ppt
a basic java programming and data type.ppta basic java programming and data type.ppt
a basic java programming and data type.ppt
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docxCharles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
 
London SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error HandlingLondon SF Developers: Custom Lightning Component Error Handling
London SF Developers: Custom Lightning Component Error Handling
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6
 
Dbms 2011
Dbms 2011Dbms 2011
Dbms 2011
 

Recently uploaded

The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 

Recently uploaded (20)

The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 

PL/SQL EXCEPTION HANDLING

  • 2. Errors • Two types of errors can be found in a program: compilation errors and runtime errors. • There is a special section in a PL/SQL block that handles the runtime errors. • This section is called the exception-handling section, and in it, runtime errors are referred to as exceptions. • The exception-handling section allows programmers to specify what actions should be taken when a specific exception occurs.
  • 3. How to handle Exception • In order to handle run time errors in the program, an exception handler must be added. • The exception-handling section has the following structure: EXCEPTION WHEN EXCEPTION_NAME THEN ERROR-PROCESSING STATEMENTS; • The exception-handling section is placed after the executable section of the block.
  • 4. How exception can be classified???  Build-in exception  Userdefined exception
  • 5. BUILT-IN EXCEPTIONS  When a built-in exception occurs, it is said to be raised implicitly.  In other words, if a program breaks an Oracle rule, the control is passed to the exception- handling section of the block.  At this point, the error processing statements are executed.  It is important for you to realize that after the exception-handling section of the block has executed, the block terminates.  Control will not return to the executable section of this block.
  • 6. This example produces the following output: There is no such student PL/SQL procedure successfully completed. Why??? Because there is no record in the STUDENT table with student ID 101, the SELECT INTO statement does not return any rows. As a result, control passes to the exception-handling section of the block, and the error message “There is no such student” is displayed on the screen. Even though there is a DBMS_OUTPUT.PUT_LINE statement right after the SELECT statement, it will not be executed because control has been transferred to the exception-handling section. Example DECLARE v_student_name VARCHAR2(50); BEGIN SELECT first_name||‘ ’||last_name INTO v_student_name FROM student WHERE student_id = 101; DBMS_OUTPUT.PUT_LINE (‘Student name is’||v_student_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE (‘There is no such student’); END;
  • 7. What are predefined exception??  The list shown below explains some commonly used predefined exceptions and how they are raised:  NO_DATA_FOUND This exception is raised when a SELECT INTO statement, which makes no calls to group functions, such as SUM or COUNT, does not return any rows.  For example, you issue a SELECT INTO statement against STUDENT table where student ID equals 101.  If there is no record in the STUDENT table passing this criteria (student ID equals 101), the NO_DATA_FOUND exception is raised.
  • 8. TOO_MANY_ROWS  This exception is raised when a SELECT INTO statement returns more than one row.  By definition, a SELECT INTO can return only single row.  If a SELECT INTO statement returns more than one row, the definition of the SELECT INTO statement is violated.  This causes the TOO_MANY_ROWS exception to be raised.  For example, you issue a SELECT INTO statement against the STUDENT table for a specific zip code.  There is a big chance that this SELECT statement will return more than one row because many students can live in the same zip code area.
  • 9. ZERO_DIVIDE  This exception is raised when a division operation is performed in the program and a divisor is equal to zero.  Previous example in the illustrates how this exception is raised. LOGIN_DENIED  This exception is raised when a user is trying to login on to Oracle with invalid username or password.
  • 10. PROGRAM_ERROR  This exception is raised when a PL/SQL program has an internal problem VALUE_ERROR  This exception is raised when conversion or size mismatch error occurs.  For example, you select student’s last name into a variable that has been defined as VARCHAR2(5).  If student’s last name contains more than five characters, VALUE_ERROR exception is raised.
  • 11. DUP_VALUE_ON_INDEX  This exception is raised when a program tries to store a duplicate value in the column or columns that have a unique index defined on them.  For example, you are trying to insert a record into the SECTION table for the course number “25,” section 1.  If a record for the given course and section numbers already exists in the SECTION table, DUP_VAL_ON_INDEX exception is raised because these columns have a unique index defined on them.
  • 12. Example FROM enrollment WHERE student_id = v_student_id; DBMS_OUTPUT.PUT_LINE ('Student is registered for '||v_total||' course(s)'); */inner block*/EXCEPTION WHEN VALUE_ERROR OR INVALID_NUMBER THEN DBMS_OUTPUT.PUT_LINE('An error has occurred'); END; */outer block */ EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('There is no such student'); END;
  • 13. • The inner block has structure similar to the outer block. • It has a SELECT INTO statement and an exception section to handle errors. • When VALUE_ERROR or INVALID_NUMBER error occurs in the inner block, the exception is raised. • It is important that you realize that exceptions VALUE_ERROR and INVALID_NUMBER have been defined for the inner block only. • Therefore, they can be raised in the inner block only. • If one of these errors occurs in the outer block, this program will be unable to terminate successfully.
  • 14. User Defined Exceptions • Often in programs you may need to handle problems that are specific to the program you write. • For example, your program asks a user to enter a value for student_id. This value is then assigned to the variable v_student_id that is used later in the program. • Generally, you want a positive number for an id. By mistake, the user enters a negative number. • However, no error has occurred because student_id has been defined as a number, and the user has supplied a legitimate numeric value. • Therefore, you may want to implement your own exception to handle this situation.
  • 15. How to declare user-defined exception • A user-defined exception is declared in the declarative 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 built-in exceptions
  • 16. Raising Exception • 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;
  • 17. example declare e exception; vamt number(10); begin select amt into vamt from ac_det where acno=124; vamt:=vamt-77000; if(vamt<2000) then raise e; end if; exception when e then dbms_output.put_line('Minimum balance amount conflict'); * end; SQL> / Declare exception if the particular data is not found Raise exception ‘E’ If ‘E’ is exception the it will be displayed as ‘minimum amount conflict’