16.2 User-Supplied Error Handlers
It is good programming practice to anticipate certain errors
and provide your own error handlers for them. User-written
error handlers allow you to handle errors for a specified
block of program statements as well as complete program
units. Any program module can contain one or more error
handlers. These error handlers test the error condition and
include statements to be executed if an error occurs.
To provide your own error handlers, you use WHEN ERROR
constructs. A WHEN ERROR construct consists of two blocks
of code: a protected region and a handler. A protected re-
gion is a block of code that is monitored by the compiler for
the occurrence of an error. A handler is the block of code
that receives program control when an error occurs during
the execution of the statements in the protected region.
There are two forms of WHEN ERROR constructs; in both
cases the protected region begins immediately after a WHEN
ERROR statement. The following partial programs illustrate
each form. In Example 16-1, the handler is attached to the
protected region, while in Example 16-2, the handler catch_
handler is detached and must be provided elsewhere in the
program unit.
The following sections further explain the concepts of pro-
tected regions and handlers.