Try OpenEdge Now
skip to main content
Error Handling
Introduction to Condition Handling : Structured error handling : What is structured error handling?
 

What is structured error handling?

Structured error handling is an error handling model found in many languages, but is typically associated with object-oriented languages. It is characterized by the following features:
*Represents all errors as objects (instances of a class)
*Allows you to define your own error types (classes)
*Allows you to explicitly raise (throw) an error
*Allows you to handle (catch) particular error types in a particular context (ABL block)
*Allows you to propagate (re-throw) errors from the current context (inner block) to the immediate outer context (outer block). In other words, when an error occurs in a called context, you can direct the calling context to handle the error
*Allows you to specify code that executes at the conclusion of some associated code, whether or not the associated code executed successfully (FINALLY block)
Structured error handling has several syntactical variations, but you may know it by one common syntax: try and catch. (Italics indicate conceptual language elements as opposed to actual ABL elements.) Examine this pseudo code:
TRY /* to execute this code */
{    
WRITE FILE myAddresses.
}

CATCH errorFileLocked /* and if the errorFileLocked error is thrown
                           (raised), let the error handling behavior here
                           catch (handle) the error instead of the system's
                           default behavior. */
{
       SEND MESSAGE "File currently unavailable."
}

CATCH errorNoWritePermission /* or if the errorNoWritePermission error
                                  is thrown, use this behavior.*/
{
       SEND MESSAGE "You do not have permission to write files."
}
The try statement provides the ability to define a block of code to which you can associate error handling behavior. The catch statement allows you to define an error handler and associate it with an error type. When the system detects an error in a try block, it will execute the code in the catch block that matches the error. What does it do if there is no matching catch block? The behavior in this scenario is language dependent, but most often, the call stack is unwound until the system finds a suitable catch block.
Thus, structured error handling turns an error condition into a named object that can be more than just an identifier and message. It provides structure by encouraging code to be defined in blocks associated with alternate, error-handling code paths (catch blocks). The syntactical structure provides an easy to read map of your error handling behavior.
This description of structured error handling describes the basic mechanism. Other features and default behaviors vary by language. The next step is to introduce the basic mechanism of ABL structured error handling before beginning a more thorough description in the rest of this manual.