start page | rating of books | rating of authors | reviews | copyrights

Oracle PL/SQL Programming, 2nd Edition

Oracle PL/SQL Programming, 2nd EditionSearch this book
Previous: 8.1 Why Exception Handling? Chapter 8
Exception Handlers
Next: 8.3 Types of Exceptions
 

8.2 The Exception Section

A PL/SQL block (of which procedures, functions, and anonymous blocks are all instances) consists of up to four parts: the header, declaration section, execution section, and exception section, as shown in the following anonymous block:

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

When an exception is raised within the execution section of a PL/SQL block, control passes to the exception section. PL/SQL then scans through the exception handlers to see if that exception is handled.

The syntax for an exception section follows:

EXCEPTION    WHEN exception_name [ OR exception_name ... ]    THEN       <executable statements> END;

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

The Exception Section

An English-like Translation

EXCEPTION    WHEN NO_DATA_FOUND    THEN       executable_statements1;

If the NO_DATA_FOUND exception was 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 a WHEN 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.

The WHEN OTHERS clause is optional; if it is not present, then any unhandled exception is immediately raised in the enclosing block, if any.

If the exception is not handled by any PL/SQL block, then the error number and message are presented directly to the user of the application. The exception is, in other words, unhandled and it disrupts the execution of the application.


Previous: 8.1 Why Exception Handling? Oracle PL/SQL Programming, 2nd Edition Next: 8.3 Types of Exceptions
8.1 Why Exception Handling? Book Index 8.3 Types of Exceptions

The Oracle Library Navigation

Copyright (c) 2000 O'Reilly & Associates. All rights reserved.

Library Home Oracle PL/SQL Programming, 2nd. Ed. Guide to Oracle 8i Features Oracle Built-in Packages Advanced PL/SQL Programming with Packages Oracle Web Applications Oracle PL/SQL Language Pocket Reference Oracle PL/SQL Built-ins Pocket Reference