.


:




:

































 

 

 

 


Handling Exceptions




Once an exception is raised, the current PL/SQL block stops its regular execution and transfers control to the exception section. The exception is then either handled by an exception handler in the current PL/SQL block or passed to the enclosing block.

To handle or trap an exception once it is raised, you must write an exception handler for that exception. In your code, your exception handlers must appear after all the executable statements in your program but before the END statement of the block.The EXCEPTION keyword indicates the start of the exception section and the individual exception handlers:

DECLARE... declarations ...BEGIN... executable statements ...[ EXCEPTION... exception handlers ... ]END;

The syntax for an exception handler is as follows:

WHEN exception_name [ OR exception_name ... ]THEN executable statements

or:

WHEN OTHERSTHEN executable statements

You can have multiple exception handlers in a single exception section. The exception handlers are structured much like a conditional CASE statement, as shown in the following table:

Exception section English-like translation
EXCEPTION WHEN NO_DATA_FOUND THEN executable_statements1 ; If the NO_DATA_FOUND exception is raised, then execute the first set of statements.
WHEN payment_overdue THEN executable_statements2 ; If the payment is overdue, then execute the second set of statements.
WHEN OTHERS THEN executable_statements3 ;END; If any other exception is encountered, then execute the third set of statements.

An exception is handled if an exception that is named in aWHEN clause matches the exception that was raised. Notice that the WHEN clause traps errors only by exception name, not by error codes. If a match is found, then the executable statements associated with that exception are run. If the exception that has been raised is not handled or does not match any of the named exceptions, the executable statements associated with the WHEN OTHERS clause (if present) will be run. Only one exception handler can catch a particular error. After the statements for that handler are executed, control passes immediately out of the block.

The WHEN OTHERS clause is optional; if it is not present, then any unhandled exception is immediately propagated back to the enclosing block, if any. The WHEN OTHERS clause must be the last exception handler in the exception section. If you place any other WHEN clauses after WHEN OTHERS, you will receive the following compilation error:

PLS-00370: OTHERS handler must be last among the exception handlers of a block




:


: 2015-10-01; !; : 396 |


:

:

,
==> ...

1928 - | 1849 -


© 2015-2024 lektsii.org - -

: 0.007 .