Free Training


C Language  |  CSS  |  MainFrame  |  VBScript  |  PHP  |  XML  |  C++ Tutorials  |  Ajax  |  JavaScript  |  CSS3  |  UML  |  jQuery  |  Microsoft AJAX

Oracle 9i Tutorials

 
Home Tutorials Oracle 9i
 

Tutorial 11 : Oracle 9i: Exception Handling

 
This week tutorial covers Oracle Exception Handling and the different types in it with sample SQL scripts along with the screen shots. Topics covered in this week, Introduction to Exception Handling - Propagation of Errors Types of Exceptions - Named System Exceptions; Unnamed System Exceptions; User-Defined Exceptions.


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.




Download Sample Scripts



Read Next: Tutorial 12: Oracle 9i: PL/SQL Collections



 

 

Comments


sathish24_606 said:

  informative............,excellent work
July 11, 2006, 8:55 am

jam said:

  Nice learning and nice to see such a website
I have add this site to my favorites !
December 28, 2006, 5:35 am

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%)
January 19, 2007, 5:01 pm

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
May 14, 2007, 1:52 am

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
August 18, 2007, 1:32 am

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 !
August 29, 2007, 9:17 pm

Mukarram Shah said:

  This is a very nifty, clean, and simple explanation of oracle exceptions. Good Job.
September 28, 2007, 11:32 am

HamzeH said:

  thank you very much
this is what I need to understand

many thanks
December 6, 2007, 2:15 am

Abhijit_shinde said:

  This is most useful info I have fount on web.
Cheers Team.
Keep it up.
January 7, 2008, 5:29 am

Brutus I said:

  I feel still Explination is needed on Exception handling in Procedural PLSQL Block.....
February 22, 2008, 4:03 am

GirishD said:

  Thanks, this is really Good as a start...
Thanks again!!
June 12, 2008, 7:47 am

Gaivij said:

  This explaination is really simple and catchy. Thanks
August 19, 2008, 1:20 am

Gaivij said:

  This explanation is really simple, catchy and informative.
Thanks :-)
August 19, 2008, 1:22 am

Aishwarya_V said:

  Can you show me how to handle when a table does not exist
September 15, 2008, 5:14 pm

arikatla said:

  Keep posting!
October 10, 2008, 12:41 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe