小编Sig*_*dur的帖子

如何在创建FileStream时处理异常

我有这样的功能,我想重构

   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 …
Run Code Online (Sandbox Code Playgroud)

delphi exception-handling tfilestream

3
推荐指数
1
解决办法
4736
查看次数

标签 统计

delphi ×1

exception-handling ×1

tfilestream ×1