HP Pascal/iX Programmer's Guide (31502-90023)

11- 6
TRY-RECOVER and Optimization
If the OPTIMIZE compiler option is used with the TRY-RECOVER construct,
the following information explains what will or will not work at
different levels.
* If an ESCAPE is done in the TRY block, or in any procedure called
from within the TRY block, all values on the left side of an
assignment statement, appearing before an ESCAPE or a procedure
call, are stored.
* If a trap occurs instead of an ESCAPE, the above statement is not
true.
Example
The following example uses the local variable flag to indicate how far
the program gets before an error. It is used to undo or unlock a
resource.
$standard_level 'ext_modcal'$
$ovflcheck off$
program dick;
type iptr=^integer;
procedure lock; external;
procedure plock $alias 'lock'$; begin end;
procedure proc(j:integer;p:iptr);
var flag: {$VOLATILE$} boolean;
i:integer;
begin
flag:=false;
try
lock;
flag:=true;
i:=maxint;
i:=i + j + p^;
if j < 0 then escape(i);
recover
begin
if not flag then halt(1); { should not halt }
end;
end;
begin
proc(1,nil);
end.
This program does not work correctly with optimization because the store
to the variable flag is done after the trap. To run the program
correctly, use $VOLATILE$ so that flag is stored before the trap occurs.
See Chapter 12 for more information on the optimizer.
Assert Procedure
The predefined procedure
assert
allows your program to test assumptions,
specify invariant conditions, and check data structure integrity.
Syntax
assert (
b, i
[,
p
])
Parameters
b
A Boolean expression that
assert
evaluates. If its value is
true,
the program executes the statement following the call to
assert
. If its value is
false,
the program's action depends upon
whether
p
is specified and whether the ASSERT_HALT compiler
option is OFF or ON (see Figure 11-1 ).