SlideShare a Scribd company logo
5
Most read
10
Most read
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

PPTX
Error handling and debugging in vb
PDF
Object-Oriented Programming in Java.pdf
PPTX
Control Statements in Java
PPT
1 - Introduction to PL/SQL
PPT
Php Error Handling
PDF
Exception Handling
PPTX
Classes, objects in JAVA
PPTX
Formatted Console I/O Operations in C++
Error handling and debugging in vb
Object-Oriented Programming in Java.pdf
Control Statements in Java
1 - Introduction to PL/SQL
Php Error Handling
Exception Handling
Classes, objects in JAVA
Formatted Console I/O Operations in C++

What's hot (20)

PPTX
Abstract Base Class and Polymorphism in C++
PPTX
Java Server Pages(jsp)
PPTX
Inheritance in java
DOCX
Placement management system
PPS
Java Exception handling
PPT
SQLITE Android
PPT
04 Handling Exceptions
PPTX
Abstraction in java [abstract classes and Interfaces
PPTX
Abstraction java
PDF
Procedure and Function in PLSQL
PPTX
Program control instructions
PPT
Operator & Expression in c++
PDF
Learn C# Programming - Data Types & Type Conversion
PPTX
PPTX
oops concept in java | object oriented programming in java
PPTX
ASP.NET Page Life Cycle
PPTX
Form using html and java script validation
PPT
Database Triggers
PPSX
Seminar on java
PPTX
Abstract class and Interface
Abstract Base Class and Polymorphism in C++
Java Server Pages(jsp)
Inheritance in java
Placement management system
Java Exception handling
SQLITE Android
04 Handling Exceptions
Abstraction in java [abstract classes and Interfaces
Abstraction java
Procedure and Function in PLSQL
Program control instructions
Operator & Expression in c++
Learn C# Programming - Data Types & Type Conversion
oops concept in java | object oriented programming in java
ASP.NET Page Life Cycle
Form using html and java script validation
Database Triggers
Seminar on java
Abstract class and Interface
Ad

Similar to Different Kinds of Exception in DBMS (20)

PPT
Introduction to PLSQL.PPT
PPTX
PL_SQL_1.pptx fvbxcfbhxdfgh .
PPTX
Module 4-packages and exceptions in java.pptx
PPTX
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PPTX
Plsql guide 2
PPT
a basic java programming and data type.ppt
DOC
Subqueries views stored procedures_triggers_transactions
DOC
3963066 pl-sql-notes-only
PPTX
Exception
PPTX
Exception
PPTX
CSL101_Ch1.pptx
PPTX
CSL101_Ch1.pptx
PPT
CSL101_Ch1.ppt
PPT
CSL101_Ch1.ppt
PPT
skill lab-java interview preparation questions.ppt
PPTX
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
PPTX
3130703_DBMS_GTU_Study_Material_Presentations_Unit-10_17102019083650AM.pptx
DOCX
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
Introduction to PLSQL.PPT
PL_SQL_1.pptx fvbxcfbhxdfgh .
Module 4-packages and exceptions in java.pptx
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
Plsql guide 2
a basic java programming and data type.ppt
Subqueries views stored procedures_triggers_transactions
3963066 pl-sql-notes-only
Exception
Exception
CSL101_Ch1.pptx
CSL101_Ch1.pptx
CSL101_Ch1.ppt
CSL101_Ch1.ppt
skill lab-java interview preparation questions.ppt
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
3130703_DBMS_GTU_Study_Material_Presentations_Unit-10_17102019083650AM.pptx
Charles WilliamsCS362Unit 3 Discussion BoardStructured Query Langu.docx
Ad

Recently uploaded (20)

PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
System and Network Administration Chapter 2
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
medical staffing services at VALiNTRY
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
top salesforce developer skills in 2025.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
System and Network Administraation Chapter 3
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
VVF-Customer-Presentation2025-Ver1.9.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
System and Network Administration Chapter 2
Reimagine Home Health with the Power of Agentic AI​
wealthsignaloriginal-com-DS-text-... (1).pdf
medical staffing services at VALiNTRY
How Creative Agencies Leverage Project Management Software.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
top salesforce developer skills in 2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Wondershare Filmora 15 Crack With Activation Key [2025
System and Network Administraation Chapter 3
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

Different Kinds of Exception in DBMS

  • 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’