| iMatix home page | Libero home page | Libero documentation | << | < | > | >> |
Libero Version 2.32 |
Exceptions are a powerful technique that make your dialog leaner and meaner. In a typical dialog many actions do some work that may result in an error condition. For instance, an action that reads some data from a file may discover that the file is no longer accessible due to a 'hard' error (protection, disk crash, insufficient memory,...). Without exceptions, the only way to handle such possibilities is to define additional states - for instance, 'after-reading-data', that check the outcome of each action with a questionable outcome.
This quickly leads to dialogs with dozens of states that serve only to keep track of these minority cases. In the majority of cases, the errors never happen. This means that the main logic of the dialog can get diluted by all the extraneous states. With exceptions, you write the dialog as though errors were incidental. When an error happens, you treat it as a special case, as an 'exception'. Otherwise, the dialog carries on normally.
Most real dialogs use exceptions, sometimes heavily, for two reasons:
A module raises an exception by setting the variable Exception-Raised to a 'true' value. The module then supplies an event in The-Exception-Event. Quite often I use an event called 'Exception' when I use an exception event to handle fatal errors. This is how we raise an exception using an event called Exception-Event in various languages:
C and C++:
exception_raised = TRUE; the_exception_event = exception_event;
80x86 assembler:
mov exception_raised,1 mov the_exception_event,exception_event
Korn Shell:
exception_raised=TRUE the_exception_event=$exception_event
COBOL:
MOVE "YES" TO EXCEPTION-RAISED MOVE EXCEPTION-EVENT TO THE-EXCEPTION-EVENT
Visual Basic:
exception_raised = True the_exception_event = exception_event
In most languages (except COBOL) Libero provides a function or macro 'Raise-Exception' that does the above. See the later sections on using Libero in each language for details.
The dialog normally executes all the action modules for an event-in-state, one by one, then switches to the next state for the event. An exception interrupts this flow. The dialog handles exceptions in two steps:
When a module raises an exception, the dialog does not care what the next state was supposed to be. This is no longer important, since the exception event will determine its own next state.
Libero also allows a short-cut: if the exception event is the same as the current event, you don't need to supply it. If you just set Exception-Raised but not the event, the dialog loops back to the same event in the same state. Honestly, I don't use this very often. I prefer to separate exception events from 'normal' events.
Take the example of three actions which the dialog executes in series. First, it reads some data from a file. Then it parses and converts the data. Lastly, it writes the data to another file. Each of these actions has a normal outcome, and possible exceptional outcomes (error during reading, invalid data, error during writing, etc.). If we write the dialog without exceptions, we need these three states:
Before-Reading: (--) Ok -> After-Reading + Read-Data-From-Input (--) Error -> + Terminate-The-Program After-Reading: (--) Ok -> After-Parsing + Parse-and-Convert-Data (--) Error -> + Terminate-The-Program After-Parsing: (--) Ok -> Another-State + Write-Data-To-Output + ... (--) Error -> + Terminate-The-Program
In this case, Read-Data-From-Input does something like this (I use C as the language):
MODULE read_data_from_input (void) { if (read_from_file (file, input) != NULL) the_next_event = ok_event; else the_next_event = error_event; }
By using an exception event (here, Error), we can collapse the three states into a single state:
Before-Reading: (--) Ok -> Another-State + Read-Data-From-Input + Parse-and-Convert-Data + Write-Data-To-Output + ... (--) Error -> + Terminate-The-Program
Read-Data-From-Input now does this:
MODULE read_data_from_input (void) { if (read_from_file (file, input) == NULL) raise_exception (error_event); }
Some hints about using exception events:
| << | <
| > | >>
| Using Libero | The Dialog File Syntax | Libero Options | Using Exceptions | Using The Defaults State | Using Sub-Dialogs | Using Super-States | Using Templates | Using Get-External-Event | Efficiency | Care and Feeding of Dialogs | When To Use Libero |
Copyright © 1996-97 iMatix |