You can use EPILOG or EXCEPTION procedures to perform cleanup if a process is terminated abnormally.
Using EPILOG Procedures
An EPILOG procedure is a special type of procedure that is available only in ALGOL, DCALGOL, DMALGOL, and NEWP. An EPILOG procedure is executed whenever the block that declares it is exited, even if the exit was caused by the process being discontinued. The EPILOG procedure can be designed to perform cleanup actions, such as liberating or causing an event.
EPILOG procedures can be declared in procedures, structure blocks, or connection blocks. The use of EPILOG procedures in connection blocks is discussed under Using PROLOG and EPILOG Procedures in Using Libraries. The use of EPILOG procedures in structure blocks is discussed in the ALGOL Programming Reference Manual, Volume 1: Basic Implementation. The remainder of this discussion assumes that the EPILOG procedure is declared in a procedure.
An EPILOG procedure can determine whether the procedure exit is normal, or whether the process is being discontinued, by inspecting the STATUS, HISTORYTYPE, HISTORYCAUSE, and HISTORYREASON task attributes of the MYSELF task variable. You can design the EPILOG procedure to take different actions, depending on whether the block exit is normal.
Note that the EPILOG procedure can be discontinued and, thus, prevented from completing all its cleanup functions. For example, if you enter two DS commands for a process, the first causes the EPILOG procedure to be entered. The second DS command discontinues the EPILOG procedure if it has not yet finished execution. This problem should rarely occur if the EPILOG procedure is kept brief.
Using EXCEPTION Procedures
If you need to ensure that certain actions are always performed when a procedure is exited abnormally, you can use an EXCEPTION procedure instead of an EPILOG procedure. EXCEPTION procedures are available in ALGOL, DCALGOL, DMALGOL, and NEWP. These procedures serve a similar function to EPILOG procedures. However, an EXCEPTION procedure is executed only if the block that declares it is exited abnormally, whereas an EPILOG procedure is executed even if the block exit is normal. Block exits are considered abnormal in either of the following cases:
-
The block is exited because of a bad GO TO statement. A bad GO TO statement transfers control to a label outside the block and can occur in the block that declared the EXCEPTION procedure, or in a nested block within that block.
-
The block is exited because the process was discontinued, because of either an operator DS (Discontinue) system command or an internal fault.
Another important feature of EXCEPTION procedures is that you can prevent them from being interrupted. To do this, you simply add the PROTECTED clause to the EXCEPTION procedure declaration. If the block that declares a protected EXCEPTION procedure is exited abnormally, then the EXCEPTION procedure executes in protected mode. A protected EXCEPTION procedure cannot be interrupted by the DS (Discontinue) or ST (Stop) system command, or by stack stretches.
The PROTECTED form of the EXCEPTION procedure is only available in NEWP and DMALGOL, and in NEWP is only permitted in blocks marked with the UNSAFE block directive. The compiler marks the resulting object code file as nonexecutable. To enable execution of the object code file, an operator can use either of the following commands:
-
The MP <file title> + EXECUTABLE form of the MP (Mark Program) system command. This command removes nonexecutable status so that the object code file can be initiated with the normal process initiation statements such as CALL, PROCESS, and RUN.
-
The SL <function name> = <file title> form of the SL (Support Library) system command. This command enables a nonexecutable object code file to be initiated by the library linkage mechanism when the object code file is linked to by function.
| Note: | An unsafe DMALGOL program is only nonexecutable if the security option DMALGOLUNSAFE is set. |
If you want an EXCEPTION procedure to be executed before any block exit, normal or abnormal, you can include an explicit call on the EXCEPTION procedure in the block. The following is an example:
700 PROCEDURE P1;
710 BEGIN
720 FILE MYFILE(KIND=DISK);
730 PROTECTED EXCEPTION PROCEDURE CLEANUP;
740 BEGIN
750 CLOSE(MYFILE,LOCK);
760 END;
.
.
.
900 CLEANUP;
910 END;The vertical ellipses in this example denote lines that are omitted because they are not essential to the point being illustrated. If P1 exits normally, then the EXCEPTION procedure CLEANUP is explicitly invoked by the statement at line 900. Note that in this case, CLEANUP is executed without protected status. If P1 exits abnormally, the system automatically invokes CLEANUP and executes it with protected status.

