加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 03:50:29 所属栏目:大数据 来源:网络整理
导读:我经常使用以下代码: function GetNumber(Handle : THandle) : Integer;beginFLock.BeginRead;try if FMap.TryGetValue(Handle,Object) then raise EArgumentException.Create('Invalid handle'); Result := Object.Number;finally FLock.EndRead;end;end;
我经常使用以下代码:
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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读