delphi - 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;
这种行为改变了吗?
解决方案
编译器生成的代码的行为没有改变。您的代码将在所有版本的编译器中以相同的方式执行。
但是,10.1 中的提示和警告是错误的。这些是 10.2 修复的编译器错误。第一个示例不应生成警告 W1035。第二个示例应生成提示 H2077。
推荐阅读
- c# - C#、ASP.Net Core 和 Linq:处理空日期
- c# - ASP.NET Core 中的持久内存并发字典
- c - 如何将多个内存文件写入C中的一个存档文件?
- javascript - 事件处理程序定义说明
- go - gob:类型不匹配:没有匹配的字段编译解码器 - Golang
- loops - 如何在 VBA Excel 中集体搜索范围内的两个值
- r - .jfindClass(as.character(driverClass)[1])中的R错误:java.lang.ClassNotFoundException
- javascript - Rails 5 Bootstrap 4 Simple_Form 文件上传前的图像预览
- javascript - 如何创建具有下一个 ID 号的行
- c++ - 当提供第二个时,首先删除哪个 shared_ptr/object?