0

Hi I am new to using Delphi and am trying to write an application that will check to see if a website is up or if there is any thing wrong with it. I am using Indy's IdHTT. The problem is that it will catch any protocol errors but not things like socket errors.

procedure TWebSiteStatus.Button1Click(Sender: TObject);
  var
    http : TIdHTTP;
    url : string;
    code : integer;
  begin
     url := 'http://www.'+Edit1.Text;
     http := TIdHTTP.Create(nil);
     try
       try
         http.Head(url);
         code := http.ResponseCode;
       except
         on E: EIdHTTPProtocolException do
           code := http.ResponseCode; 
         end;
         ShowMessage(IntToStr(code));
         if code <> 200 then
         begin
           Edit2.Text:='Something is wrong with the website';
           down;
         end;
     finally
       http.Free();
     end;
  end;

I am basically trying to catch any thing that is not that the website is ok so I can call another form that will setup an email to tell me that the site is down.

update: First you are right I did miss that 'then' sorry about that was removing other code and it got deleted by mistake. I did not know the specific to general when dealing with exceptions thank you. Finally I did find what i was looking for was this code here

on E: EIdSocketError do    

using the uses IdStack

NicholasSPI
  • 13
  • 1
  • 6
  • 1
    Note that you've got a syntax error: `if code <> 200 begin` whould be `if ... then begin` – Johan Oct 08 '13 at 20:39
  • 3
    It's easier to comment on your real code. Please don't post fake code. – David Heffernan Oct 08 '13 at 20:46
  • 1
    Please copy and paste real code. Please also pay attention to indentation: Only the first three lines of code are really part of the `except` block. The rest are really inside the try-finally block, despite the indentation suggesting otherwise. – Rob Kennedy Oct 08 '13 at 21:31
  • possible duplicate of [How to check URL with IdHTTP?](http://stackoverflow.com/questions/13950676/how-to-check-url-with-idhttp) – TLama Dec 14 '13 at 07:40

1 Answers1

5

Change your code to either catch all exceptions, or add more specific ones as well:

url := 'http://www.'+Edit1.Text;
http := TIdHTTP.Create(nil);
try
  try
    http.Head(url);
    code := http.ResponseCode;
  except
    on E: EIdHTTPProtocolException do
    begin
      code := http.ResponseCode; 
      ShowMessage(IntToStr(code));
      if code <> 200 
      begin
        Edit2.Text:='Something is wrong with the website';
        down;
      end;
    end;
    // Other specific Indy (EId*) exceptions if wanted
    on E: Exception do
    begin
      ShowMessage(E.Message);
    end;
  end;  // Added missing end here.
finally
  http.Free();
end;

Note that if you're going to handle multiple exception types, it's important to go from most specific to least specific. In other words, if you put less specific (more general types) exceptions first, this is what happens:

try
  DoSomethingThatCanRaiseAnException();
except
  on E: Exception do
    ShowMessage('This one fires always (covers all exceptions)');
  on E: EConvertError do
    ShowMessage('This one will never happen - never gets this far');
end;

This one will work properly, because it's more specific to less specific. Properly, it would be reversed:

try
  DoSomethingThatCanRaiseAnException();
except
  on E: EConvertError do
    ShowMessage('This one gets all EConvertError exceptions');
  on E: Exception do
    ShowMessage('This one catches all types except EConvertError');
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444