Oracle 9i Tutorials
Tutorials
Oracle 9iTutorial 11 : Oracle 9i: Exception Handling
Exceptions
An Exception is an error situation, which arises during program execution. When an error occurs exception is raised, normal execution is stopped and control transfers to exception-handling part. Exception handlers are routines written to handle the exception. The exceptions can be internally defined (system-defined or pre-defined) or User-defined exception.
Predefined exception is raised automatically whenever there is a violation of Oracle coding rules. Predefined exceptions are those like ZERO_DIVIDE, which is raised automatically when we try to divide a number by zero. Other built-in exceptions are given below. You can handle unexpected Oracle errors using OTHERS handler. It can handle all raised exceptions that are not handled by any other handler. It must always be written as the last handler in exception block.
- CURSOR_ALREADY_OPEN – Raised when we try to open an already open cursor.
- DUP_VAL_ON_INDEX – When you try to insert a duplicate value into a unique column
- INVALID_CURSOR – It occurs when we try accessing an invalid cursor
- INVALID_NUMBER – On usage of something other than number in place of number value.
- LOGIN_DENIED – At the time when user login is denied
- TOO_MANY_ROWS – When a select query returns more than one row and the destination variable can take only single value.
- VALUE_ERROR – When an arithmetic, value conversion, truncation, or constraint error occurs.
Predefined exception handlers are declared globally in package STANDARD. Hence we need not have to define them rather just use them.
The biggest advantage of exception handling is it improves readability and reliability of the code. Errors from many statements of code can be handles with a single handler. Instead of checking for an error at every point we can just add an exception handler and if any exception is raised it is handled by that.
For checking errors at a specific spot it is always better to have those statements in a separate begin – end block.
Examples: Following example gives the usage of ZERO_DIVIDE exception 
Exmpmple 2: I have explained the usage of NO_DATA_FOUND exception in the following 
The DUP_VAL_ON_INDEX is raised when a SQL statement tries to create a duplicate value in a column on which a primary key or unique constraints are defined.
Example to demonstrate the exception DUP_VAL_ON_INDEX.

More than one Exception can be written in a single handler as shown below.
EXCEPTION
When NO_DATA_FOUND or TOO_MANY_ROWS then
Statements;
END;
User-defined Exceptions
A User-defined exception has to be defined by the programmer. User-defined exceptions are declared in the declaration section with their type as exception. They must be raised explicitly using RAISE statement, unlike pre-defined exceptions that are raised implicitly. RAISE statement can also be used to raise internal exceptions.
Declaring Exception:
DECLARE
myexception EXCEPTION;
BEGIN
------
Raising Exception:
BEGIN
RAISE myexception;
-------
Handling Exception:
BEGIN
------
----
EXCEPTION
WHEN myexception THEN
Statements;
END;
Points To Ponder:
- An Exception cannot be declared twice in the same block.
- Exceptions declared in a block are considered as local to that block and global to its sub-blocks.
- An enclosing block cannot access Exceptions declared in its sub-block. Where as it possible for a sub-block to refer its enclosing Exceptions.
The following example explains the usage of User-defined Exception

RAISE_APPLICATION_ERROR
To display your own error messages one can use the built-in RAISE_APPLICATION_ERROR. They display the error message in the same way as Oracle errors. You should use a negative number between –20000 to –20999 for the error_number and the error message should not exceed 512 characters.
The syntax to call raise_application_error is
RAISE_APPLICATION_ERROR (error_number, error_message, { TRUE | FALSE });

Propagation of Exceptions
When an exception is raised and corresponding handler is not found in the current block then it propagates to the enclosing blocks till a handler is found. If a handler is not found in its enclosing blocks also then it raises an unhandled exception error to the host environment.
Exceptions cannot be propagated across remote procedure calls. i.e. a PL/SQL program cannot catch exceptions raised by remote subprograms
Reraising exceptions
When you want an exception to be handles in the current block as well in its enclosing block then you need to use RAISE statement without an exception name.

Comments
sathish24_606 said:
| informative............,excellent work |
jam said:
|
Nice learning and nice to see such a website I have add this site to my favorites ! |
D Thomas said:
|
This is just what I have been looking for...examples I can understand. Can you show an example of updating multiple records using cursor and/or fetch. Example, update salary for all employees due to new rate increase of 6%) |
asifcse said:
|
Exceptions cannot be propagated across remote procedure calls. i.e. a PL/SQL program cannot catch exceptions raised by remote subprograms let me know wht does remote means here is it a object in other schema..if so than i have verified that my pl block catched exception raised in subprogram. Thanks Asif |
Mohammad Idrees Hussain said:
|
This is really very good tutorial, and very impressive work mentioned with examples that helps me to understand the p roblem with logic. Thnks |
Gasoline said:
| This is the most smartest tutorial i've been seen int the world. Excellent explanations. Smart & Simple examples, script. Nothing I can say but THANKKKKKKSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS ! |
Mukarram Shah said:
| This is a very nifty, clean, and simple explanation of oracle exceptions. Good Job. |
HamzeH said:
|
thank you very much this is what I need to understand many thanks |
Abhijit_shinde said:
|
This is most useful info I have fount on web. Cheers Team. Keep it up. |
Brutus I said:
| I feel still Explination is needed on Exception handling in Procedural PLSQL Block..... |
GirishD said:
|
Thanks, this is really Good as a start... Thanks again!! |
Gaivij said:
| This explaination is really simple and catchy. Thanks |
Gaivij said:
|
This explanation is really simple, catchy and informative. Thanks :-) |
Aishwarya_V said:
| Can you show me how to handle when a table does not exist |
arikatla said:
| Keep posting! |
