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

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

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

  1. [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语句移至自己的过程.

  1. function GetNumber(Handle : THandle) : Integer;
  2. procedure InvHandle;
  3. begin
  4. raise EArgumentException.Create('Invalid handle');
  5. end;
  6. begin
  7. FLock.BeginRead;
  8. try
  9. if FMap.TryGetValue(Handle,Object) then
  10. InvHandle;
  11. Result := Object.Number;
  12. finally
  13. FLock.EndRead;
  14. end;
  15. end;

猜你在找的Delphi相关文章