诊断一段小程序(10分)

  • 主题发起人 唐晓锋
  • 开始时间

唐晓锋

Unregistered / Unconfirmed
GUEST, unregistred user!
try
HttpReg:=TRegistry.Create;
HttpReg.RootKey:=HKey_Current_User;
AppKey:='/Software/BlindSystem/'+'盲人语音浏览器';
if not HttpReg.OpenKey(AppKey,True) then
raise Exception.Create(Format(' Create Key : "%s" Error',[AppKey]));
finally
HttpReg.Free;
为什么编译的时候有[Warning] Main.pas(126): Variable 'HttpReg' might not have been initialized
 
试试这样:
HttpReg:=TRegistry.Create;
try
HttpReg.RootKey:=HKey_Current_User;
AppKey:='/Software/BlindSystem/'+'盲人语音浏览器';
if not HttpReg.OpenKey(AppKey,True) then
raise Exception.Create(Format(' Create Key : "%s" Error',[AppKey]));
finally
HttpReg.Free;
end;
 
HttpReg:=TRegistry.Create;
HttpReg.RootKey:=HKey_Current_User;
AppKey:='/Software/BlindSystem/'+'盲人语音浏览器';
if not HttpReg.OpenKey(AppKey,True) then
raise Exception.Create(Format(' Create Key : "%s" Error',[AppKey]));
HttpReg.Free;
不会有警告
但是当您把第一行语句注释掉
{ HttpReg:=TRegistry.Create;
}
...
就会有警告
Delphi认为try中的语句可能会因为异常条件而不被执行,
所以当finally中用到了HTTPReg时, 有可能没有正确初始化
在您的程序里面, 有可能Create就产生异常, 所以Free时就会
出问题
 
这里Try语句是用来检测HttpReg:=TRegistry.Create的异常情况的,
因此Create应在Try之前! :)
 
接受答案了.
 
顶部