13.3 The try...nally statement

A Try..Finally statement has the following form:

_________________________________________________________________________________________________________ Try...nally statement
-- --trystatement- try- statement list-  nally-   nally statements- end---------

-- --  nally statements-statementlist--------------------------------------
___________________________________________________________________

If no exception occurs inside the statement List, then the program runs as if the Try, Finally and End keywords were not present.

If, however, an exception occurs, the program ow is immediatly transferred from the point where the excepion was raised to the rst statement of the Finally statements.

All statements after the nally keyword will be executed, and then the exception will be automatically re-raised. Any statements between the place where the exception was raised and the rst statement of the Finally Statements are skipped.

As an example consider the following routine:

Procedure Doit (Name : string);  
Var F : Text;  
begin  
  Try  
    Assign (F,Name);  
    Rewrite (name);  
    ... File handling ...  
  Finally  
    Close(F);  
  end;

If during the execution of the le handling an execption occurs, then program ow will continue at the close(F) statement, skipping any le operations that might follow between the place where the exception was raised, and the Close statement. If no exception occurred, all le operations will be executed, and the le will be closed at the end.