首页 > 解决方案 > Delphi try..finally 退出 10.1 和 10.2 版本之间的行为变化

问题描述

我维护了一个必须在多个 Delphi 版本中运行的 Delphi 组件。在最近的几个版本中,我注意到行为发生了变化。

以下代码在 Delphi 10.1 中给出警告,在 Delphi 10.2 中编译良好:

[dcc32 警告] asdf.pas(1179): W1035 函数“TSomeClass.SomeFunc”的返回值可能未定义

function TSomeClass.SomeFunc(objc: TObject; const xD: array of string): integer;
var
  s: string;
  i: Integer;
begin
  try
    repeat
      s := ReadLn;

      // more code here

      for i := 0 to High(xD) do
      begin
        if s = xD[i] then
        begin
          // Result := 0;
          exit;
        end;
      end;

      // more code here

    until False;
  finally
    Result := 0;
  end;
end;

以下代码在 Delphi 10.2 中给出了提示,并在 Delphi 10.1 中编译良好:

[dcc32 提示] asdf.pas(1179): H2077 分配给“TSomeClass.SomeFunc”的值从未使用过

function TSomeClass.SomeFunc(objc: TObject; const xD: array of string): integer;
var
  s: string;
  i: Integer;
begin
  try
    repeat
      s := ReadLn;

      // more code here

      for i := 0 to High(xD) do
      begin
        if s = xD[i] then
        begin
          Result := 0;
          exit;
        end;
      end;

      // more code here

    until False;
  finally
    Result := 0;
  end;
end;

这种行为改变了吗?

标签: delphidelphi-10.1-berlindelphi-10.2-tokyo

解决方案


编译器生成的代码的行为没有改变。您的代码将在所有版本的编译器中以相同的方式执行。

但是,10.1 中的提示和警告是错误的。这些是 10.2 修复的编译器错误。第一个示例不应生成警告 W1035。第二个示例应生成提示 H2077。


推荐阅读