title: Is it safe to assume InternetCloseHandle() won't fail, thereby allowing cleaner code?
body: <p>Here's a routine to do an HTTP request using WinINet and either return the fetched string or raise an exception:</p>
  
  <pre><code>function Request(const pConnection: HINTERNET; const localpath: string): string;
  var Buffer: packed Array[1..5000] of Char; BytesRead: Cardinal; pRequest: HINTERNET;     sent: boolean;
  begin
  Result := '';
  pRequest := HTTPOpenRequest(pConnection, 'GET', pchar(localpath), nil, nil, nil, 0, 0);
  if pRequest &lt;&gt; nil then
    begin
    sent := HTTPSendRequest(pRequest, nil, 0, nil, 0);
    if sent then
      while InternetReadFile(pRequest, @Buffer, SizeOf(Buffer)-1 {leave room for terminator}, BytesRead) do
        begin
        Buffer[BytesRead+1] := #0;
        Result := Result + buffer;
        end;
    InternetCloseHandle(pRequest);
    if not sent then RaiseLastOSerror; // HTTPSendRequest failed
    end
  else RaiseLastOSerror; // HTTPOpenRequest failed
  end;
  </code></pre>
  
  <p>If InternetCloseHandle(pRequest) can fail even though pRequest was successfully assigned, GetLastError() will return an error code for InternetCloseHandle() instead of HTTPSendRequest().  Fixing that would require code like:</p>
  
  <pre><code>function Request(const pConnection: HINTERNET; const localpath: string): string;
  var Buffer: packed Array[1..5000] of Char; BytesRead: Cardinal; pRequest: HINTERNET;
  begin
  Result := '';
  pRequest := HTTPOpenRequest(pConnection, 'GET', pchar(localpath), nil, nil, nil, 0, 0);
  if pRequest &lt;&gt; nil then
    begin
    if HTTPSendRequest(pRequest, nil, 0, nil, 0) then
      while InternetReadFile(pRequest, @Buffer, SizeOf(Buffer)-1 {leave room for terminator}, BytesRead) do
        begin
        Buffer[BytesRead+1] := #0;
        Result := Result + buffer;
        end
    else
      begin
      InternetCloseHandle(pRequest);
      RaiseLastOSerror; // HTTPSendRequest failed
      end;
    InternetCloseHandle(pRequest);
    end
  else RaiseLastOSerror; // HTTPOpenRequest failed
  end;
  </code></pre>
  
  <p>but that seems a lot uglier and more confusing at first glance.</p>
  
  <p>Is it safe to assume InternetCloseHandle() won't fail, thereby allowing the simpler code?</p>