 |
|
User-Defined Exceptions
Oracle Tips by
Burleson
|
PL/SQL allows developers to define their own exceptions. This is a
useful method of returning a specific status to calling procedures and
functions. Giving user-defined exceptions meaningful names helps make
code easier to understand. User-defined exceptions must be explicitly
raised through the use of the RAISE statement. Listing 2.16
illustrates how user-defined exceptions are declared and raised.
Listing 2.16 A user-defined exception.
DECLARE
xStudentNotEligibleForAid EXCEPTION;
...
IF <condition> THEN
RAISE xStudentNotEligibleForAid;
END IF;
An exception raised using the RAISE statement is processed
just like any exception raised by Oracle.
The OTHERS Exception Handler
The OTHERS exception handler functions as a catch-all
exception handler for a PL/SQL block. The OTHERS exception
handler is used as follows:
WHEN OTHERS THEN
System_Error_Handler (<parameters>);
Care must be taken when raising user-defined exceptions in a block
that uses the OTHERS exception handler. If the user-defined
exception is to be raised to a calling procedure, an exception handler
for the user-defined exception should be defined that explicitly
re-raises the exception, as follows:
EXCEPTION
WHEN StudentNotEligibleForAid THEN
RAISE;
WHEN OTHERS THEN
System_Error_Handler (<parameters>);
The OTHERS exception handler should follow any other
exception handlers. Any exception not handled by another exception
handler is handled by the OTHERS exception handler.
This is an
excerpt from the book "High Performance Oracle Database
Automation" by Jonathan Ingram and Donald K. Burleson, Series
Editor. |