0% found this document useful (0 votes)
15 views14 pages

Exception Handling

Uploaded by

P S k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

Exception Handling

Uploaded by

P S k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

EXCEPTION

HANDLING
What is an exception:
 An exception is an error condition

during a program execution.


 An error or a warning event is called

an exceptions.
What is exception handling?
 The mechanism for solving such an

exceptions is called exception


handling.
 PL/SQL supports programmers to catch
such condition using exceptions block
in the program and an appropriate
action is taken against the error
condition.

 Types of exception:
 1. Pre-defined /System-defined

Exceptions
 2. User- Defined Exceptions
System-defined exceptions/Predefine
exceptions/Built-in exceptions:
 System defined exception are defined and

maintained implicitly by the oracle server.


 These exceptions are mainly defined in

the oracle STANDARD package.


 System defined exceptions are thrown by

default.
 For example, the predefined exception

NO_DATA_FOUND is raised when a


SELECT INTO statement returns no rows.
The following table lists few of the
important pre-defined exceptions
Syntax for Predefined Exception
Handling:
The general syntax for exception handling is as
follows.
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
WHEN exception3 THEN
exception3-handling-statements
........
WHEN others THEN
exception3-handling-statements
END;
USER DEFINED EXCEPTIONS:
 User defined Exceptions are raised
exlicitly in the body of the PL/SQL block.
 The developers can build their own

exceptions and use them for handling


errors.
 They can be created in the declaration

part of a subprogram.
 PL/SQL allow us to define our own

exception according to the need of our


program.
 A user defined exception must be

declared and then raised explicitly.


 Syntax for User defined Exception:

DECLARE
exception_name
EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;
EXCEPTION
WHEN exception_name
THEN statement;
END;
CURSORS
 A cursor is a pointer to this context area.
PL/SQL controls the context area through a
cursor.
 A cursor is a temporary work area created in

system memory when a SQL statement is


executed.
 A cursor is a set of rows together with a

pointer that identifies a current row. It is a


database object to retrieve data from a
result set one row at a time.
 It is useful when we want to manipulate
the record of a table in a singleton
method, in other words one row at a
time.
 In other words, a cursor can hold more than
one row, but can process only one row at a
time.
 The set of rows the cursor holds is called the

active set.
Each cursor contains the followings 4
steps-
1. Declare Cursor: In this part we declare
variables and return a set of values.
2. Open: This is the entering part of the cursor.
3. Fetch: Used to retrieve the data row by row
from a cursor.
4. Close: This is an exit part of the cursor and
used to close a cursor.
Example:

Declare
enumemp.eno%type;
enemp.ename%type;
Cursor cur is select eno, ename from emp where
jobname = “mgr”;
Begin
Open cur;
Loop Fetch cur into enum,en;
Exit when cur%NOTFOUND;
Dbms_output.put_line(„emp num ‟||enum||‟ emp
name „||en);
End loop;
Close cur;
End;
/
Each There are two types of cursors:
1.Implicit cursors
2.Explicit cursors

You might also like