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:

   ... declarations ...
   ... executable statements ...
   ... exception handlers ... ]

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:

   WHEN exception_name [ OR exception_name ... ]
      <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 below:

The Exception Section

An English-like Translation


If the NO_DATA_FOUND exception was raised, then execute the first set of statements.

   WHEN payment_overdue

If the payment is overdue, then execute the second set of statements.


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 EditionNext: 8.3 Types of Exceptions
8.1 Why Exception Handling?Book Index8.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
This HTML Help has been published using the chm2web software.