【问题标题】:Delphi try..finally exit behavior change between versions 10.1 and 10.2Delphi try..finally 退出版本 10.1 和 10.2 之间的行为变化
【发布时间】:2020-08-28 15:12:48
【问题描述】:

我维护了一个必须在多个 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 Hint] 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;

这种行为是否改变了?

【问题讨论】:

    标签: delphi delphi-10.1-berlin delphi-10.2-tokyo


    【解决方案1】:

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

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

    【讨论】:

    • 也许是这个错误报告“QC39805 W1035 函数的返回值......可能未定义有时是错误的”。不幸的是,这些报告无法再访问了。
    猜你喜欢
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 2011-02-13
    • 2013-09-01
    • 2011-02-20
    相关资源
    最近更新 更多