Statements [ HP Pascal/iX Reference Manual ] MPE/iX 5.0 Documentation
HP Pascal/iX Reference Manual
Statements
TRY-RECOVER
A Pascal program that encounters a run-time error is aborted. Because
this is not always acceptable, the system programming extensions define
the TRY-RECOVER structured statement that allows the user to trap all
run-time errors.
The predefined procedure escape allows the user to cause a run-time error
to occur, and the predefined function escapecode allows the user to
determine the last type of error that occurred. See the section "Error
Handling Routines" for more information on escape and escapecode.
Syntax
The statement following the reserved word RECOVER may have a statement
label. One can jump to such a label only from within the RECOVER
statement itself.
The types of errors that are trapped by TRY-RECOVER are:
* All Pascal run-time errors (defined in Appendix A ).
* An implementation defined set of hardware errors.
* An implementation defined set of operating system detected errors.
* All user-generated error conditions (generated by calling escape).
Upon detecting an error in the execution of the body of a TRY-RECOVER
statement (the statements between the reserved words TRY and RECOVER, as
well as any procedures and functions called from such statements), the
following sequence of events occurs:
* The escape code, indicating the type of error that occurred, is
saved for later retrieval by the predefined function escapecode.
* The run-time environment is restored to the environment of the
most recent TRY-RECOVER statement. This may involve prematurely
exiting any nested procedure and function calls and closing any
open files local to those routines.
* Execution is transferred to the statement following the reserved
word RECOVER.
If no errors are detected within the body of the TRY-RECOVER statement,
the recover statement is skipped, and execution continues at the first
statement following the TRY-RECOVER statement.
The TRY-RECOVER statement does not trap errors in its recover part (the
statement following the reserved word RECOVER). If an error occurs in the
execution of the recover part, execution is transferred to the recover
part of an enclosing TRY-RECOVER statement. If there is no enclosing
TRY-RECOVER statement the program aborts.
The semantics of the TRY-RECOVER statement do not guarantee that the
effects of any statements executed in the body of the TRY are valid when
executing the RECOVER statement. Certain implementations, however, may
guarantee that the effects of any executed statements are valid. Certain
other implementations may provide the user with a method of indicating
that certain variables preserve their value. See the HP Pascal/iX
Programmer's Guide or the HP Pascal/HP-UX Programmer's Guide, depending
on your implementation, for more details. Also see the compiler option
"VOLATILE" .
Note that when execution is transferred to the RECOVER statement, the
environment in which the error occurred no longer exists. If that
environment is required to perform error handling, then trap handlers are
required. See the chapter on Error Recovery in the HP Pascal/iX
Programmer's Guide or in the HP Pascal/HP-UX Programmer's Guide,
depending on your implementation, for more information.
Example
TRY
open( f, 'filename' );
RECOVER BEGIN
writeln( 'open failed' );
...
END;
The above code fragment prevents a program from aborting if a file cannot
be opened.
Example
PROCEDURE proc1;
BEGIN
... { errors will be trapped in try 0 }
TRY {try 1}
... { errors will be trapped here in try 1 }
RECOVER BEGIN
... { errors will be trapped in try 0 }
END;
... { errors will be trapped in try 0 }
END;
...
BEGIN
... { errors will abort the program }
TRY {try 0}
... { errors will be trapped here in try 0 }
proc1;
... { errors will be trapped here in try 0 }
RECOVER BEGIN
... { errors will abort the program }
END;
... { errors will abort the program }
END.
In the previous example, any errors occurring in the TRY-RECOVER in proc1
cause execution to be transferred to the recover part of the try
statement in proc1. Any errors occurring in the TRY-RECOVER in the outer
block, in the recover statement in proc1, and outside of the TRY-RECOVER
in proc1 cause execution to be transferred to the recover part of the try
statement in the outer block. Any error occurring in the recover
statement in the outer block and outside of the TRY-RECOVER statement in
outer block, aborts the program because there is no TRY-RECOVER to catch
the error.
Example
VAR
int : integer;
...
int := 0;
TRY
...
int := 1;
...
int := 2;
...
int := 3;
...
RECOVER BEGIN
...
END;
If execution is transferred to the recover statement, there is no
guarantee that the variable int has a value other than zero for the
following reasons:
* The error could have occurred anywhere within the try body. The
first assignment to int may not have been executed yet.
* Even if an assignment statement was executed, the semantics do not
guarantee that the actual location of int was updated. If the new
value of int was stored in a location other than its memory
location, then the transfer of execution to the RECOVER statement
does not update the memory location of int.
MPE/iX 5.0 Documentation