Using the RAISE
Oracle Tips by
You can cause exceptions to be raised in your code by using the
RAISE statement. Listing 4.23 illustrates the use of the
Listing 4.23 Using the RAISE statement in your code.
IF <some condition> THEN
WHEN xABORT_PROCEDURE THEN
Itís a good idea not to explicitly raise the predefined Oracle
exceptions. Doing so confuses the debugging process. Consider the
StudentClasses_rec IN StudentClasses_cur LOOP
WHERE course_number = iCourseID;
iTotalClasses := iTotalClasses + 1;
IF (iTotalClasses = 0) THEN
If your code looks like this example and youíre
receiving a NO_DATA_FOUND
exception, you canít be certain where the exception is originating
without doing some extra debugging work. It would be much better to
define an appropriately named user-defined exception, say xSTUDENT_HAS_NO_CLASSES,
and explicitly raise that exception instead.
If you must raise an exception
under a certain condition, take advantage of the power and flexibility
of user-defined exceptions.
Oracle provides the Raise_Application_Error() procedure to
allow you to raise custom error numbers within your applications. You
can generate errors and their associated text starting with -20000 and
proceeding through -20999 (a grand total of 1,000 error numbers that
you can use). Listing 4.24 illustrates the use of the
Listing 4.24 Using the Raise_Application_Error()
Balance integer := 24;
IF (nBalance <= 100) THEN
Raise_Application_Error (-20343, 'The balance is too low.');
In this example, error number -20343 is raised if the value of
nBalance isnít greater than 100, yielding a message that looks
ORA-20343: The balance is too low.
This is an excerpt from the book "High Performance Oracle
Database Automation" by Jonathan Ingram and Donald K.
Burleson, Series Editor.