gpt4 book ai didi

delphi - 编译器警告 "return value might be undefined"

转载 作者:行者123 更新时间:2023-12-03 14:39:05 25 4
gpt4 key购买 nike

我经常使用以下代码:

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 它对我来说没有意义。

问题:

  • 发出警告有什么原因吗?
  • 我怎样才能摆脱它(将 Result := Object.Number 行移出锁不是一个选项,我想避免编写完全不必要的 Result : = 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;

关于delphi - 编译器警告 "return value might be undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6570224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com