我有一个这样的函数,我想重构它

   function Myfunction(sUrl, sFile: String) : Boolean;
    var
      GetData : TFileStream;
    begin
      Result := False;
      //if the line below fails, I get an unhandled exception
      GetData := TFileStream.Create(sFile, fmOpenWrite or fmCreate);
      try        
        try
          IdHTTP.Get(sUrl, GetData);
          Result := (IdHTTP.ResponseCode = 200);
        except
          on E: Exception do begin
            MessageBox(0, PChar(E.message), 'Niðurhala skrá', MB_ICONERROR or MB_OK);
          end;
        end;
      finally
        GetData.Free;
      end;
    end;

    Procedure SomeOtherCode;
     Begin
        //How can I best defend against the unhandled exception above
        //unless the call to the function is packed in a try .. except block
        //the code jumps skips the if statement an goes to next 
        //exception block on the stack
        if MyFunction('http://domain.com/file.html', 'c:\folder\file.html') then
            ShowMessage('Got the file')
         else
            ShowMessage('Error !');
        End
     end;

问题:

请参阅上面过程 SomeOtherCode 中的注释。

此致

有帮助吗?

解决方案

只需将您想在尝试中捕获异常的代码。

function MyFunction(...): Boolean;
var
  Stream: TFileStream;
begin
  Result := False;
  try
    Stream := TFileStream.Create(...);
    try
      // more code
      Result := ...
    finally
      Stream.Free;
    end;
  except
    // handle exception
  end
end;

其他提示

关于异常处理的要点有两个:

  • finally 用于资源清理;你在业务逻辑中经常看到这种情况
  • except 用于对特定异常做出反应(并通过函数结果和中间变量摆脱状态逻辑);你很难在业务逻辑中看到它

在你的情况下:

Myfunction 不应返回布尔值,也不应包含 except 阻止,并且不执行 MessageBox 但只是让异常传播。
SomeOtherCode 应包含 except 阻止并告诉用户出了什么问题。

例子:

procedure Myfunction(sUrl, sFile: String);
var
  GetData: TFileStream;
begin
  Result := False;
  //if the line below fails, I get an unhandled exception
  GetData := TFileStream.Create(sFile, fmOpenWrite or fmCreate);
  try        
    IdHTTP.Get(sUrl, GetData);
    if (IdHTTP.ResponseCode <> 200) <> then
      raise Exception.CreateFmt('Download of %s failed, return code %d', [sURl, IdHTTP.ResponseCode]);
  finally
    GetData.Free;
  end;
end;

procedure SomeOtherCode:
begin
  try
    MyFunction('http://domain.com/file.html', 'c:\folder\file.html');
  except
    on E: Exception do begin
      MessageBox(0, PChar(E.message), 'Niðurhala skrá', MB_ICONERROR or MB_OK);
    end;
  end;
end;

现在代码更加清晰:

  • 业务逻辑中不再有 UI
  • 你的一个地方 except 正在处理中
  • 所有失败都会得到同等处理(cannot create file, download failure)

祝你好运。

——杰罗恩

一个非常流行的解决方案是避免完全“成功”或“失败”返回值。而不是功能,而是使用一个过程并使用异常来处理故障:

procedure Download(sUrl, sFile: String);

接着

try
  Download ('http://domain.com/file.html', 'c:\folder\file.html');
  ShowMessage('Got the file')
except
  on E:Exxx do 
  begin
    // handle exception
    ShowMessage('Error !');
  end
end;

这也具有没有人可以调用功能并默默地忽略返回值的效果。

如果您希望您的功能向用户显示消息并在任何故障上返回false,请按以下方式进行编码:

function Myfunction(sUrl, sFile: String) : Boolean;
var
  GetData : TFileStream;
begin
  Result := False;
  try
    //if the line below fails, I get an unhandled exception
    GetData := TFileStream.Create(sFile, fmOpenWrite or fmCreate);
    try        
      try
        IdHTTP.Get(sUrl, GetData);
        Result := (IdHTTP.ResponseCode = 200);
      except
        on E: Exception do begin
          MessageBox(0, PChar(E.message), 'Niðurhala skrá', MB_ICONERROR or MB_OK);
        end;
      end;
    finally
      GetData.Free;
    end;
  except
    // you can handle specific exceptions (like file creation errors) or any exception here
  end;
end;

警告恕我直言,此设计是混合业务逻辑(例如从Internet获取资源/文件并将其保存到文件)和用户界面逻辑(例如在错误时向用户显示消息)。

通常,是一种更好的方法,可以将业务与UI逻辑分开,因为您的代码可重复使用。

例如,您可能想重新系数:

function DownloadToAFile(const sUrl, sFile: string): boolean;
var
  GetData : TFileStream;
begin
  GetData := TFileStream.Create(sFile, fmOpenWrite or fmCreate);
  try        
    IdHTTP.Get(sUrl, GetData);
    Result := (IdHTTP.ResponseCode = 200);
  finally
    GetData.Free;
  end;
end;

function UIDownloadToAFile(const sUrl, sFile: string): boolean;
begin
  try
    Result := DownloadToAFile(sURL, sFile);
  except
    on E: EIDException do //IndyError
      MessageBox(0, PChar(E.message), 'Internet Error', MB_ICONERROR or MB_OK);
    on E: EFileCreateError do //just can't remember the extact class name for this error
      MessageBox(0, PChar(E.message), 'File create Error', MB_ICONERROR or MB_OK);
  end;
end;

procedure SomeOtherCode:
begin
  if UIDownloadToAFile('http://domain.com/file.html', 'c:\folder\file.html') then
    ShowMessage('Got the file')
   else
     ShowMessage('Error !');
end;

明天,如果您正在编写服务或DataSnap模块,则可以自由使用下载toafile,或者可能会随时写入新的ServiceDownloadToafile Wich,转到日志或Windows事件,或者也许发送电子邮件通知Hostadmin关于它。

你应该只使用一个 try 并获取您的所有功能代码。

由于某些原因,大多数人滥用了整合的组合。正确的序列是

try 
  // allocate resource here
  try 
  finally
    // free resource here
  end;
except
  // handle exception here
end;

这使您可以在构造函数和破坏器中捕获异常。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top