6

I tried with Delphi XE and I got Not Responding while compiling. Does it work in your computer or is there something wrong with the function?

function Test(const FileName: string;
  const Force: boolean = false): boolean;
var
  IsAllowed: boolean;
begin
  result := false;
  if FileExists(FileName) then
  begin
    try
      if (Force) then
      begin
        result := false;
        exit;
      end;
    finally
      if IsAllowed then
        DeleteFile(FileName);
    end;

    try
      result := true;
    except
      result := false;
    end;
  end;
end;
Kara
  • 6,115
  • 16
  • 50
  • 57
user
  • 681
  • 3
  • 10
  • 19

1 Answers1

11

It compiles on my computer. Though I get the warning W1036 Variable 'IsAllowed' might not have been initialized.

Update: I can reproduce the hang when I include Windows in the uses clause. Subbmitted to Quality Central: QC93806.

program hang_test;

{$APPTYPE CONSOLE}

uses
  // Windows, // uncomment to include Windows -> hang on compile
  SysUtils;

function Test(const FileName: string; const Force: boolean = false): boolean;
  // your function here

begin
  try

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

It looks like a bug; you should report it in the Quality Central.

Update 2: Minimal case which reproducibly hangs the compiler:

function HangCompiler: Boolean;
begin
  try
    Exit; // 1. exit from a try..finally
  finally
    DeleteFile(''); // 2. inlined function call in finally (include Windows to inline)
  end;
  // 3. try..except
  try
    Result := True;
  except
    Result := False;
  end;
end;
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128