Exception-Handling Concepts and Terminology

There are, in general, two types of exceptions:

 

System exception

An exception that is defined by Oracle and is usually raised by the PL/SQL runtime engine when it detects an error condition. Some system exceptions have names, such as NO_DATA_FOUND, while many others simply have numbers and descriptions.

 

Programmer-defined exception

An exception that is defined by the programmer and is therefore specific to the application at hand. You can associate exception names with specific Oracle errors using the EXCEPTION_INIT pragma (a compiler directive, requesting a specific behavior), or you can assign a number and description to that error usingRAISE_APPLICATION_ERROR.

The following terms will be used throughout this chapter:

 

Exception section

The optional section in a PL/SQL block (anonymous block, procedure, function, trigger, or initialization section of a package) that contains one or more "handlers" for exceptions. The structure of an exception section is very similar to a CASE statement, which we discussed in Chapter 4.

 

Raise

Stops execution of the current PL/SQL block by notifying the runtime engine of an error. Oracle itself can raise exceptions, or your own code can raise an exception with either the RAISE or RAISE_APPLICATION_ERROR command.

 

Handle (used as a verb), handler (used as a noun)

"Traps" an error within an exception section. You can then write code in the handler to process that error, which might involve recording the error occurrence in a log, displaying a message to the user, or propagating an exception out of the current block.

 

Scope

The portion of code (whether in a particular block or for an entire session) in which an exception can be raised. Also, that portion of code for which an exception section can trap and handle exceptions that are raised.

 

Propagation

The process by which exceptions are passed from one block to its enclosing block if the exception goes unhandled in that block.

 

Unhandled exception

An exception is said to go "unhandled" when it propagates without being handled out of the outermost PL/SQL block. Control then passes back to the host execution environment, at which point that environment/program determines how to respond to the exception (roll back the transaction, display an error, ignore it, etc.).

 

Un-named or anonymous exception

An exception that has an error code and a description associated with it, but does not have a name that can be used in a RAISE statement or in an exception handler WHEN clause.

 

Named exception

An exception that has been given a name, either by Oracle in one of its built-in packages or by a developer. You can also associate a name with this exception through the use of the EXCEPTION_INIT pragma, or leave it defined only by its name (which can be used to both raise and handle the exception).

6.2 Defining Exceptions

Before an exception can be raised or handled, it must be defined. Oraclepredefines thousands of exceptions, mostly by assigning numbers and messages to those exceptions. Oracle also assigns names to a few of the most commonly encountered exceptions.

You can define or declare your own exceptions for use in your applications in two different ways, described in the following sections.