finally
The Finally keyword is used to mark the start of the final block of statements
in a Try statement. They are executed regardless of what happens in the Try
statements.
However, the Finally clause does not actually handle any exceptions - the
program will terminate if no Except clause is found.
Try-Finally is normally used by a routine to guarantee the cleanup processing
takes place, such as freeing resources, with the exception being correctly
passed to the caller to handle.
Try
... code which my raise an exception ...
Except
... to handle and/or report an exception ...
Finally
... code to clean-up connections, resource ...
End;
Example
Download This Source for Free Pascal
{$MODE DELPHI} // Enable Try/Finally keywords
var
number, zero : Integer;
begin
// Try to divide an integer by zero - to raise an exception
number := -1;
Try
zero := 0;
number := 1 div zero;
Writeln('number / zero = ',number);
Finally
if number = -1 then begin
Writeln('Number was not assigned a value - using default');
number := 0;
end;
end;
end.
Output
Number was not assigned a value - using default
Runtime error 200 at $080480C2
$080480C2
$0805F3F1
See Also
Except,
On,
Raise,
Try.