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.