You are on page 1of 20

PL/SQL Exception Handling

Exception
• An error occurs during the program execution
is called Exception in PL/SQL.
• PL/SQL facilitates programmers to catch such
conditions using exception block in the
program and an appropriate action is taken
against the error condition.
• There are two type of exceptions:
– System-defined Exceptions
– User-defined Exceptions
Syntax for Exception Handling
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; 
Some Predefined Exceptions
Exception Name ReasonError Number
CURSOR_ALREADY_OPEN When you open a cursor
that is already open. ORA-06511
INVALID_CURSOR When you perform an invalid
operation on a cursor like closing a
cursor, fetch data from a cursor that
is not opened. ORA-01001
NO_DATA_FOUND When a SELECT...INTO clause does
not return any row from a table. ORA-01403
TOO_MANY_ROWS When you SELECT or fetch more
than one row into a record or
variable. ORA-01422
ZERO_DIVIDE
When you attempt to divide a
number by zero. ORA-01476
Trap PreDefined Oracle Server Errors
declare
a number(3):=:a;
b number(3):=:b;
begin
dbms_output.put_line(a/b);
exception
when zero_divide then
dbms_output.put_line('division by zero is not
allowed');
end;
With SQLERRM AND SQLCODE
declare
a number(3):=8;
b number(3):=0;
begin
dbms_output.put_line(a/b);
exception
when zero_divide then
dbms_output.put_line('division by zero is
not allowed');

DBMS_OUTPUT.PUT_LINE(' error
message ' || SQLERRM);
DBMS_OUTPUT.PUT_LINE(SQLcode);

end;
declare
rec student%rowtype;
r student.rno%type;
begin
r := &r;
select * into rec from student where rno = r;
dbms_output.put_line(rec.rno||rec.name||
rec.score);
Trap PreDefined Oracle Server Errors
declare
rec student%rowtype;
r student.rno%type;
begin
r := &r;
select * into rec from student where rno = r;
dbms_output.put_line(rec.rno||rec.name||rec.score);
exception
when no_data_found then
dbms_output.put_line('this rno does not exist');
when too_many_rows then
dbms_output.put_line('too many rows');
when others then
dbms_output.put_line('error!!');
end;
Non – Defined Oracle Server Errors
• Those system exception for which oracle does not provide
a name is known as unamed system exception. These
exception do not occur frequently. These Exceptions have a
code and an associated message.
• There are two ways to handle unnamed system
exceptions: 
1. By using the WHEN OTHERS exception handler, or 
2. By associating the exception code to a name and using it
as a named exception.
• We can assign a name to unnamed system exceptions using
a Pragma called EXCEPTION_INIT. 
EXCEPTION_INIT will associate a predefined Oracle error
number to a programmer_defined exception name.
• Steps to be followed to use unnamed system
exceptions are 
• They are raised implicitly. 
• If they are not handled in WHEN Others they
must be handled explicity. 
• To handle the exception explicity, they must
be declared using Pragma EXCEPTION_INIT as
given above and handled referecing the user-
defined exception name in the exception
section.
Non defined(or unnamed) System
Exceptions
DECLARE
exception_name EXCEPTION;
PRAGMA EXCEPTION_INIT (exception_name,
Err_code);
BEGIN
Execution section
EXCEPTION
WHEN exception_name THEN
handle the exception
END;
Trap non-defined Orcale server errors
declare
large_value exception;
pragma exception_init(large_value,-01438);
begin
insert into student(rno,name)
values(11111111111111,'arnav');
exception
when large_value then
dbms_output.put_line('value is too large for the
column');
end;
Trapping User Defined Exceptions
• Apart from system exceptions we can explicitly define
exceptions based on business rules. These are known
as user-defined exceptions.
• Steps to be followed to use user-defined exceptions: 
1. They should be explicitly declared in the declaration
section. 
2. They should be explicitly raised in the Execution
Section. 
3. They should be handled by referencing the user-
defined exception name in the exception section.
Syntax for user defined exceptions
DECLARE  
   exception_name EXCEPTION;  
BEGIN  
   IF condition THEN  
      RAISE exception_name;  
   END IF;  
EXCEPTION  
   WHEN exception_name THEN  
   statement;  
END;  
To raise an exception in case a user enters roll number greater
then 30 fro records to be displayed. Consider that the class
contains only 1-30 roll numbers

declare
no_rollno exception;
r student.rno%type;
rec student%rowtype;
begin
r:=&rno;
if r>30 then
raise no_rollno;
end if;
select * into rec from student where rno = r;
dbms_output.put_line(rec.rno||' '||rec.name||' '||
rec.eng);
exception
when no_rollno then
dbms_output.put_line('roll numbers beyond 30 do no
exist');
end;
To prevent a user from entering marks
>100 with the help of a user defined
exception
declare
r student.rno%type := &r;
n student.name%type := &n;
e student.eng%type := &e;
m student.maths%type := &m;
s student.science%type := &s;
wrong_marks exception;
begin
if(e>100 or m>100 or s>100) then
raise wrong_marks;
end if;
insert into student(rno,name,eng,maths,science)
values(r,n,e,m,s);
exception
when wrong_marks then
dbms_output.put_line('marks cannot be greater than
100');
end;
Propagation of Exception
• if the exception is raised in the inner block it
should be handled in the exception block of
the inner PL/SQL block else the control moves
to the Exception block of the next upper
PL/SQL Block. If none of the blocks handle the
exception the program ends abruptly with an
error.
Raise_Application_Error Procedure
• RAISE_APPLICATION_ERROR is a built-in procedure in oracle which
is used to display the user-defined error messages along with the
error number whose range is in between -20000 and -20999.
• Whenever a message is displayed using
RAISE_APPLICATION_ERROR, all previous transactions which are
not committed within the PL/SQL Block are rolled back
automatically (i.e. change due to INSERT, UPDATE, or DELETE
statements).
• RAISE_APPLICATION_ERROR raises an exception but does not
handle it.
• RAISE_APPLICATION_ERROR is used for the following reasons, 
a) to create a unique id for an user-defined exception. 
b) to make the user-defined exception look like an Oracle error.
• The General Syntax to use this procedure is:
RAISE_APPLICATION_ERROR (error_number, error_message);

You might also like