Oracle For Beginners Page : 1
srikanthtechnologies.com
Chapter 17
EXCEPTION HANDLING
What is an exception?
How to handle exceptions?
Predefined exceptions
When NO_DATA_FOUND exception is not raised?
User-defined exception
Reraising an exception
Associating an exception With An Oracle Error
Exception propagation
When is a PL/SQL block successful or failure?
What is an Exception?
In PL/SQL, errors and warnings are called as exceptions. Whenever a predefined error occurs
in the program, PL/SQL raises an exception. For example, if you try to divide a number by
zero then PL/SQL raises an exception called ZERO_DIVIDE and if SELECT can not find a record
then PL/SQL raises exception NO_DATA_FOUND.
PL/SQL has a collection of predefined exceptions. Each exception has a name. These
exceptions are automatically raised by PL/SQL whenever the corresponding error occurs.
In addition to PL/SQL predefined exceptions, user can also create his own exceptions to deal
with errors in the applications. Understanding how to handle exception raised by PL/SQL is as
important as understanding how to write code to achieve task. Because exception handling is
an important part of any application and application is not complete without exception
handling.
How to handle exceptions?
When PL/SQL raises a predefined exception, the program is aborted by displaying error
message. But if program is to handle exception raised by PL/SQL then we have to use
Exception Handling part of the block.
Exception handling part is used to specify the statements to be executed when an
exception occurs. Control is transferred to exception handling part whenever an
exception occurs. After the exception handler is executed, control is transferred to
Oracle For Beginners Page : 2
srikanthtechnologies.com
next statement in the enclosing block. If there is no enclosing block then control
returns to Host (from where you ran the PL/SQL block).
The following is the syntax of exception handling part.
WHEN exception-1 [or exception -2] ... THEN
statements;
[WHEN exception-3 [or exception-4] ... THEN
statements; ] ...
[WHEN OTHERS THEN
statements; ]
exception-1, exception-2 are exceptions that are to be handled. These exceptions are either
pre-defined exceptions or user-defined exceptions.
The following example handles NO_DATA_FOUND exception. If SELECT statement doesn’t
retrieve any row then PL/SQL raises NO_DATA_FOUND exception, which is handled in
exception handling part.
declare
…
begin
select …
exception
when no_data_found then
statements;
end;
When two or more exceptions are given with a single WHEN then the statements are executed
whenever any of the specified exceptions occur.
The following exception handling part takes the same action when either NO_DATA_FOUND or
TOO_MANY_ROWS exceptions occur.
declare
...
begin
select ...
exception
when no_data_found or too_many_rows then
statements;
end;
Oracle For Beginners Page : 3
srikanthtechnologies.com
The following snippet handles these two exceptions in different ways.
declare
...
begin
select ...
exception
when no_data_found then
statements;
when too_many_rows then
statements;
end;
WHEN OTHERS is used to execute statements when an exception other than what are
mentioned in exception handler has occurred.
Note: If an exception is raised but not handled by exception handling part then PL/SQL block
is terminated by displaying an error message related to the exception.
Sample Programs
The following is an example of exception handler. This program assigns course fee of “C” to
course “C++”. If course “C” does not exist then it sets course fee of “C++” to average fee of
all courses.
declare
v_fee courses.fee%type;
begin
select fee into v_fee
from courses
where ccode = 'c';
update courses
set fee = v_fee
where ccode='c++';
exception
when no_data_found then
update courses
set fee = ( select avg(fee) from courses)
where ccode = 'c++';
end;
/
If SELECT cannot find a row course code “c” then it raises NO_DATA_FOUND exception. When
exception is raised, control is transferred to exception handling part and course fee of “c++” is
set to average course fee of all courses. If course code “c” is found then it sets the course fee
of course “c++” to the course fee of “c”.
Oracle For Beginners Page : 4
srikanthtechnologies.com
Getting information about error - SQLCODE and SQLERRM
In WHEN OTHERS section of exception handler, you can use SQLCODE and SQLERRM functions
to get the error number and error message respectively. As there is no predefined exception
for each of Oracle errors, you will not get a particular exception for most of the errors.
However, it is possible to know the error code and error message of the most recently
occurred error using these two functions. This is one way of knowing which Oracle error has
exactly occurred. The other method is associating an exception with an Oracle error. Please
see “Associating an exception with Oracle error” section for details.
The following example demonstrates how to use SQLCODE and SQLERRM.
declare
newccode varchar2(5) := null;
begin
update courses set ccode = newccode where ccode = 'c';
exception
when dup_val_on_index then
dbms_output.put_line('Duplicate course code');
when others then
dbms_output.put_line( sqlerrm);
end;
If you run the above program, the following output will be generated.
ORA-01407: cannot update ("BOOK"."COURSES"."CCODE") to NULL
PL/SQL procedure successfully completed.
The above output is generated by WHEN OTHERS part of exception handling part. SQLERRMS
returns the error message of the most recent error. As we are trying to set CCODE, which is a
not null column to NULL value, PL/SQL raises an exception. But as the error (-01407) is not
associated with any predefined exception, WHEN OTHERS part of exception handling part is
executed.
Note: You cannot use SQLCODE or SQLERRM directly in a SQL statement. Instead, you must
assign their values to variables then use the variables in the SQL statement.
Predefined exceptions
PL/SQL has defined certain common errors and given names to these errors, which are called
as predefined exceptions. Each exception has a corresponding Oracle error code. The
following is the list of predefined exceptions and the corresponding Oracle error code.
Oracle For Beginners Page : 5
srikanthtechnologies.com
Exception Oracle Error SQLCODE Value
ACCESS_INTO_NULL
COLLECTION_IS_NULL
CURSOR_ALREADY_OPEN
DUP_VAL_ON_INDEX
INVALID_CURSOR
INVALID_NUMBER
LOGIN_DENIED
NO_DATA_FOUND
NOT_LOGGED_ON
PROGRAM_ERROR
ROWTYPE_MISMATCH
SELF_IS_NULL
STORAGE_ERROR
SUBSCRIPT_BEYOND_COUNT
SUBSCRIPT_OUTSIDE_LIMIT
SYS_INVALID_ROWID
TIMEOUT_ON_RESOURCE
TOO_MANY_ROWS
VALUE_ERROR
ZERO_DIVIDE
ORA-06530
ORA-06531
ORA-06511
ORA-00001
ORA-01001
ORA-01722
ORA-01017
ORA-01403
ORA-01012
ORA-06501
ORA-06504
ORA-30625
ORA-06500
ORA-06533
ORA-06532
ORA-01410
ORA-00051
ORA-01422
ORA-06502
ORA-01476
-6530
-6531
-6511
-1
-1001
-1722
-1017
+100
-1012
-6501
-6504
-30625
-6500
-6533
-6532
-1410
-51
-1422
-6502
-1476
Table 1: Predefined Exceptions
The following is the description of some of the pre-defined exceptions.
CURSOR_ALREADY_OPEN Raised if you try to open an already open cursor.
DUP_VAL_ON_INDEX Raised if you try to store duplicate values in a
database column that is constrained by a unique
index.
INVALID_CURSOR Raised if you try an illegal cursor operation.
INVALID_NUMBER Raised in an SQL statement if the conversion of a
character string to a number fails because the string
does not represent a valid number.
srikanthtechnologies.com
Chapter 17
EXCEPTION HANDLING
What is an exception?
How to handle exceptions?
Predefined exceptions
When NO_DATA_FOUND exception is not raised?
User-defined exception
Reraising an exception
Associating an exception With An Oracle Error
Exception propagation
When is a PL/SQL block successful or failure?
What is an Exception?
In PL/SQL, errors and warnings are called as exceptions. Whenever a predefined error occurs
in the program, PL/SQL raises an exception. For example, if you try to divide a number by
zero then PL/SQL raises an exception called ZERO_DIVIDE and if SELECT can not find a record
then PL/SQL raises exception NO_DATA_FOUND.
PL/SQL has a collection of predefined exceptions. Each exception has a name. These
exceptions are automatically raised by PL/SQL whenever the corresponding error occurs.
In addition to PL/SQL predefined exceptions, user can also create his own exceptions to deal
with errors in the applications. Understanding how to handle exception raised by PL/SQL is as
important as understanding how to write code to achieve task. Because exception handling is
an important part of any application and application is not complete without exception
handling.
How to handle exceptions?
When PL/SQL raises a predefined exception, the program is aborted by displaying error
message. But if program is to handle exception raised by PL/SQL then we have to use
Exception Handling part of the block.
Exception handling part is used to specify the statements to be executed when an
exception occurs. Control is transferred to exception handling part whenever an
exception occurs. After the exception handler is executed, control is transferred to
Oracle For Beginners Page : 2
srikanthtechnologies.com
next statement in the enclosing block. If there is no enclosing block then control
returns to Host (from where you ran the PL/SQL block).
The following is the syntax of exception handling part.
WHEN exception-1 [or exception -2] ... THEN
statements;
[WHEN exception-3 [or exception-4] ... THEN
statements; ] ...
[WHEN OTHERS THEN
statements; ]
exception-1, exception-2 are exceptions that are to be handled. These exceptions are either
pre-defined exceptions or user-defined exceptions.
The following example handles NO_DATA_FOUND exception. If SELECT statement doesn’t
retrieve any row then PL/SQL raises NO_DATA_FOUND exception, which is handled in
exception handling part.
declare
…
begin
select …
exception
when no_data_found then
statements;
end;
When two or more exceptions are given with a single WHEN then the statements are executed
whenever any of the specified exceptions occur.
The following exception handling part takes the same action when either NO_DATA_FOUND or
TOO_MANY_ROWS exceptions occur.
declare
...
begin
select ...
exception
when no_data_found or too_many_rows then
statements;
end;
Oracle For Beginners Page : 3
srikanthtechnologies.com
The following snippet handles these two exceptions in different ways.
declare
...
begin
select ...
exception
when no_data_found then
statements;
when too_many_rows then
statements;
end;
WHEN OTHERS is used to execute statements when an exception other than what are
mentioned in exception handler has occurred.
Note: If an exception is raised but not handled by exception handling part then PL/SQL block
is terminated by displaying an error message related to the exception.
Sample Programs
The following is an example of exception handler. This program assigns course fee of “C” to
course “C++”. If course “C” does not exist then it sets course fee of “C++” to average fee of
all courses.
declare
v_fee courses.fee%type;
begin
select fee into v_fee
from courses
where ccode = 'c';
update courses
set fee = v_fee
where ccode='c++';
exception
when no_data_found then
update courses
set fee = ( select avg(fee) from courses)
where ccode = 'c++';
end;
/
If SELECT cannot find a row course code “c” then it raises NO_DATA_FOUND exception. When
exception is raised, control is transferred to exception handling part and course fee of “c++” is
set to average course fee of all courses. If course code “c” is found then it sets the course fee
of course “c++” to the course fee of “c”.
Oracle For Beginners Page : 4
srikanthtechnologies.com
Getting information about error - SQLCODE and SQLERRM
In WHEN OTHERS section of exception handler, you can use SQLCODE and SQLERRM functions
to get the error number and error message respectively. As there is no predefined exception
for each of Oracle errors, you will not get a particular exception for most of the errors.
However, it is possible to know the error code and error message of the most recently
occurred error using these two functions. This is one way of knowing which Oracle error has
exactly occurred. The other method is associating an exception with an Oracle error. Please
see “Associating an exception with Oracle error” section for details.
The following example demonstrates how to use SQLCODE and SQLERRM.
declare
newccode varchar2(5) := null;
begin
update courses set ccode = newccode where ccode = 'c';
exception
when dup_val_on_index then
dbms_output.put_line('Duplicate course code');
when others then
dbms_output.put_line( sqlerrm);
end;
If you run the above program, the following output will be generated.
ORA-01407: cannot update ("BOOK"."COURSES"."CCODE") to NULL
PL/SQL procedure successfully completed.
The above output is generated by WHEN OTHERS part of exception handling part. SQLERRMS
returns the error message of the most recent error. As we are trying to set CCODE, which is a
not null column to NULL value, PL/SQL raises an exception. But as the error (-01407) is not
associated with any predefined exception, WHEN OTHERS part of exception handling part is
executed.
Note: You cannot use SQLCODE or SQLERRM directly in a SQL statement. Instead, you must
assign their values to variables then use the variables in the SQL statement.
Predefined exceptions
PL/SQL has defined certain common errors and given names to these errors, which are called
as predefined exceptions. Each exception has a corresponding Oracle error code. The
following is the list of predefined exceptions and the corresponding Oracle error code.
Oracle For Beginners Page : 5
srikanthtechnologies.com
Exception Oracle Error SQLCODE Value
ACCESS_INTO_NULL
COLLECTION_IS_NULL
CURSOR_ALREADY_OPEN
DUP_VAL_ON_INDEX
INVALID_CURSOR
INVALID_NUMBER
LOGIN_DENIED
NO_DATA_FOUND
NOT_LOGGED_ON
PROGRAM_ERROR
ROWTYPE_MISMATCH
SELF_IS_NULL
STORAGE_ERROR
SUBSCRIPT_BEYOND_COUNT
SUBSCRIPT_OUTSIDE_LIMIT
SYS_INVALID_ROWID
TIMEOUT_ON_RESOURCE
TOO_MANY_ROWS
VALUE_ERROR
ZERO_DIVIDE
ORA-06530
ORA-06531
ORA-06511
ORA-00001
ORA-01001
ORA-01722
ORA-01017
ORA-01403
ORA-01012
ORA-06501
ORA-06504
ORA-30625
ORA-06500
ORA-06533
ORA-06532
ORA-01410
ORA-00051
ORA-01422
ORA-06502
ORA-01476
-6530
-6531
-6511
-1
-1001
-1722
-1017
+100
-1012
-6501
-6504
-30625
-6500
-6533
-6532
-1410
-51
-1422
-6502
-1476
Table 1: Predefined Exceptions
The following is the description of some of the pre-defined exceptions.
CURSOR_ALREADY_OPEN Raised if you try to open an already open cursor.
DUP_VAL_ON_INDEX Raised if you try to store duplicate values in a
database column that is constrained by a unique
index.
INVALID_CURSOR Raised if you try an illegal cursor operation.
INVALID_NUMBER Raised in an SQL statement if the conversion of a
character string to a number fails because the string
does not represent a valid number.