delphi – 编译器警告“返回值可能未定义”

前端之家收集整理的这篇文章主要介绍了delphi – 编译器警告“返回值可能未定义”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我经常使用以下代码
function GetNumber(Handle : THandle) : Integer;
begin
FLock.BeginRead;
try
  if FMap.TryGetValue(Handle,Object) then
    raise EArgumentException.Create('Invalid handle');
  Result := Object.Number;
finally
  FLock.EndRead;
end;
end;

不幸的是编译器给了我所有这些方法的警告:

[DCC Warning] Unit.pas(1012): W1035 Return value of function 'GetNumber' might be undefined

我知道这个警告,但在这种情况下,我根本看不出任何理由.还是有一个我失踪的场景会导致一个未定义的结果值?我知道在try..except的情况下的警告,但是try..finally对我来说没有意义.

问题:

>有什么理由警告吗?
>如何摆脱它(将结果:= Object.Number行移出锁定不是一个选项,我想避免在每个函数的顶部写一个完全不必要的结果:= 0行)

谢谢!

解决方法

Is there any reason for the warning?

我看不到一个,但是因为加注而在那里

How can I get rid of it (moving the Result := Object.Name line out of
the lock is not an option,and I want
to avoid writing an completely
unncessary Result := 0 line at the top
of each function)

将raise语句移至自己的过程.

function GetNumber(Handle : THandle) : Integer;
    procedure InvHandle;
    begin
        raise EArgumentException.Create('Invalid handle');
    end;
begin
    FLock.BeginRead;
    try
        if FMap.TryGetValue(Handle,Object) then
            InvHandle;
        Result := Object.Number;
    finally
        FLock.EndRead;
    end;
end;
原文链接:https://www.f2er.com/delphi/102537.html

猜你在找的Delphi相关文章