For a console app (run under Windows 7+), if the user improperly terminates the app by clicking the "X" in the upper right of the title bar, can the program still cleanup (e.g., close opened files, etc.)? For the following code, I couldn't get the "finalize" block to operate if the app is improperly closed at either of the first two readlns.
(There might be some way to disable the "X" but I wouldn't want to do that. If the app should hang without raising an exception, the user would then have no easy way to close the app.)
{$mode objfpc}{$H+}
{$APPTYPE CONSOLE}
program Try_Except_Finally;
uses
SysUtils, // required for type Exception
crt;
var
Debug_file: text;
begin
try
try
clrscr;
write('Press any key ... '); readln;
writeln(Debug_file, 'OK'); // create exception - file not open
except
on E: Exception do
begin
writeln('Exception block (file not open)'); readln;
end;
end;
finally // always executed, even regardless of exceptions
writeln('finally block - do cleanup here!');
readln;
end;
end.
Thanks,
Don C.