编译此函数时没有响应?

发布于 2024-11-06 11:56:29 字数 501 浏览 3 评论 0原文

我尝试使用 Delphi XE,但编译时没有响应。它在您的计算机上可以运行还是功能有问题?

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-11-13 11:56:29

它在我的电脑上编译。虽然我收到警告 W1036 变量“IsAllowed”可能尚未初始化。

更新:当我在 use 子句中包含 Windows 时,我可以重现挂起。
已提交到质量中心: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.

看起来像一个错误;您应该在质量中心中报告该问题。

更新 2:可重复挂起编译器的最小情况:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文