views:

406

answers:

2

My problem relates to the other question I have asked about loading image from the URL. The code below works. However, I have encountered the problem if I try to get the image (it suppose be there) but apparently was removed. The application does not know and returns the error. Is there a way to prevent it by capturing incoming error. I would appreciate any suggestions. It happens at his line:

 IdHTTP1.get('http://www.google.com/intl/en_ALL/images/logo.gif',MS);

Thanks, Chris

uses
  GIFImg;

 procedure TForm1.btn1Click(Sender: TObject);
 var
   MS : TMemoryStream;
   GIf: TGIFImage;
 begin
     MS := TMemoryStream.Create;
    GIf := TGIFImage.Create;
   try
    IdHTTP1.get('http://www.google.com/intl/en_ALL/images/logo.gif',MS);
    Ms.Seek(0,soFromBeginning);       
   Gif.LoadFromStream(MS);
   img1.Picture.Assign(GIF);

  finally
   FreeAndNil(GIF);
   FreeAndNil(MS);
  end;
end;
A: 

every try has a catch
it is there the place where you should treat your exceptions
probably creating a blank image with some text on it Image Not Available

pixel3cs
Well, no, in Delphi it's spelled *except*, not *catch*, and not every `try` has one. Some of them, as shown in the question, have a `finally` instead.
Rob Kennedy
+7  A: 

How do you capture the exception? The same way you capture any other exception. Use a try-except block.

procedure TForm1.btn1Click(Sender: TObject);
var
  MS : TMemoryStream;
  Gif: TGIFImage;
begin
  MS := TMemoryStream.Create;
  try
    Gif := TGIFImage.Create;
    try
      try
        IdHTTP1.Get('http://www.google.com/intl/en_ALL/images/logo.gif', MS);
      except
        on e: EIdHTTPProtocolException do begin
          if e.ErrorCode = 404 then begin
            // not found
            exit;
          end else begin
            // some other reason for failure
            raise;
          end;
        end;
      end;
      MS.Position := 0;
      Gif.LoadFromStream(MS);
      img1.Picture.Assign(Gif);
    finally
      Gif.Free;
    end;
  finally
    MS.Free;
  end;
end;

When Indy gets an unacceptable response, it raises an exception of type EIdHTTPProtocolException. It does not raise a different exception class for each possible failure reason, so check the ErrorCode property for the numeric response. Refer to the HTTP spec for the meanings of the various response codes.

It looks like Indy can be configured to ignore certain response codes via the AIgnoreReplies argument to TIdCustomHTTP.DoRequest method, but I've traced that parameter back as far as I'm willing to go today, so if you want more information about that, either ask a new question or someone can edit this answer to replace this paragraph with something more complete.

In my example above, I have checked for 404 (not found) and exit. Make sure you don't use MS if Get raises an exception because it will have indeterminate contents, and probably not a valid GIF image. For any other server response, I re-raise the exception so the caller can handle it instead. You'll probably want to change that. Any exception that's not a descendant of EIdHTTPProtocolException will get re-raised automatically. Do not catch exceptions for errors that you don't know how to resolve.

Rob Kennedy
Thank you Rob, I was not sure how to apply except and your example solved my problem. I really appreciate. Chris
Greener