PL/SQL User's Guide and Reference 10g Release 1 (10.1) Part Number B10807-01 |
|
|
View PDF |
The RAISE
statement stops normal execution of a PL/SQL block or subprogram and transfers control to an exception handler.
RAISE
statements can raise predefined exceptions, such as ZERO_DIVIDE
or NO_DATA_FOUND
, or user-defined exceptions whose names you decide. For more information, see "Defining Your Own PL/SQL Exceptions".
Syntax
Keyword and Parameter Description
A predefined or user-defined exception. For a list of the predefined exceptions, see "Summary of Predefined PL/SQL Exceptions".
Usage Notes
PL/SQL blocks and subprograms should RAISE
an exception only when an error makes it impractical to continue processing. You can code a RAISE
statement for a given exception anywhere within the scope of that exception.
When an exception is raised, if PL/SQL cannot find a handler for it in the current block, the exception propagates to successive enclosing blocks, until a handler is found or there are no more blocks to search. If no handler is found, PL/SQL returns an unhandled exception error to the host environment.
In an exception handler, you can omit the exception name in a RAISE
statement, which raises the current exception again. This technique allows you to take some initial corrective action (perhaps just logging the problem), then pass control to another handler that does more extensive correction. When an exception is reraised, the first block searched is the enclosing block, not the current block.
Example
The following example raises an exception when an inventoried part is out of stock, or when a divide-by-zero situation is about to occur:
DECLARE out_of_stock EXCEPTION; quantity_on_hand NUMBER := 0; denominator NUMBER := 0; BEGIN IF quantity_on_hand = 0 THEN RAISE out_of_stock; END IF; IF denominator = 0 THEN raise ZERO_DIVIDE; END IF; EXCEPTION WHEN out_of_stock THEN dbms_output.put_line('No more parts in stock.'); WHEN ZERO_DIVIDE THEN dbms_output.put_line('Attempt to divide by zero.'); WHEN OTHERS THEN dbms_output.put_line('Some other kind of problem...'); END; /
Related Topics