Loglan 82, A micro-manual of the programming language - Basic constructs and facilities

12) Exception handling


Exceptions are events that cause interruption of normal program execution. One kind of exceptions are those which are raised as a result of some run time errors. For instance, when an attempt is made to access a killed object, when the result of numeric operation does not lie within the range, when the dynamic storage allocated to a program is exceeded etc.

Another kind of exceptions are those which are raised explicitly by a user (with the execution of the raise statement).

The response to exceptions (one or more) is defined by an exception handler. A handler may appear at the end of declarations of any unit. The corresponding actions are defined as sequences of statements preceded by keyword when and an exception identifier.

Example:

In procedure squareeq (p.3) we wish to include the case when a=0. It may be treated as an exception (division by zero).

The handler declared in that procedure handles the only one exception (division_by_zero).

When an exception is raised, the corresponding handler is searched for, starting from the active object and going through return traces. If there is no object containing the declaration of the handler, then the program (or the corresponding process) is terminated. Otherwise the control is transferred to the first found handler.

In our example the handler is declared within the unit itself, so control is passed to a sequence:

Therefore, when b=/=0, the unique root of square equation will be determined and the procedure will be normally terminated (terminate). In general, terminate causes that all the objects are terminated, starting from that one where the exception was raised and ending on that one where the handler was found. Then the computation is continued in a normal way.

In our example, when b=0, a new exception is raised by the user. For this kind of exceptions , the exception itself should be declared (because it is not predefined as a run time error). Its declaration may have parameters which are transmitted to a handler. The exception declaration need not be visible by the exception handler. However the way the handler is searched for does not differ from the standard one. Consider an example:

Exception Wrong_data may be raised wherever its declaration (signal Wrong_data) is visible. When its handler is found the specified sequence of actions is performed. In the example above different handlers may be 0efined in inner units to the main block where squereeq is called.

The case a=0 could be included, of course, in a normal way, i.e. by a corresponding conditional statement occurring in the procedure body. But the case a=0 was assumed to be exceptional (happens scarcely). Thus the evaluation of condition a=0 would be mostly unnecessary. As can be noticed thanks to exceptions the above problem can be solved with the minimal waste of run time.




Last update 02/07/95
Comments, suggestions and critiques are welcome to : linfo062@crisv2.univ-pau.fr