Re: try.. except

Giganews Newsgroups
Subject: Re: try.. except
Posted by:  bluewolf (bluewo…@yournamehere.com)
Date: Sun, 9 Nov 2003

bluewolf <bluewo…@yournamehere.com> wrote in
news:Xns942E3948B9C94bluewolfyournamehe…@81.3.74.23:
> I want to make a news program that scan's a list of news servers and gets
> group counts on a list of groups then creates stats on the different news
> servers with the groups.
>
> My problem is I have a middle to low programmer knowlegde and don't know
> how to use the try.. except.. functions and indy keeps raising an
> exception when it can't connect to a server and when the timeout happens.
> I would like some information on how to handle the exceptions, and in the
> exception how to set a varible saying that the server is unreachable or
> it was a connection timedout.
>
> I have this, but don't know how to setup the class for the execption.
>
> ---code---
> //global vars
> type
>  //?? would this be right
>  eMyStatsConnectionError = EIdSocketErrror(class);
> end;
> var
>  ConnectErr  : Boolean;
> //global vars
>
> procedure TForm1.Button1Click(Sender: TObject);
> begin
>    // 'm' left out to raise exception.
>    IdNNTP1.Host:='news.atozedsoftware.co';
>    IdNNTP1.Port := 119;
>    Try
>      IdNNTP1.Connect(1000);
>      IdNNTP1.SelectGroup('atozedsoftware.indy.protocol.nntp');
>      ListBox1.Items.Add('Message Count: '+IntToStr(idNNTP1.MsgCount));
>      ListBox1.Items.Add('Message Hi: '+IntToStr(idNNTP1.MsgHigh));
>      ListBox1.Items.Add('Message Low: '+IntToStr(idNNTP1.MsgLow));
>      IdNNTP1.Disconnect;
>      Except
>        on e:EMyStatsConnectionError do connecterr := false;
>    end;
> end;

Found out the answer to my question..
I put IdException in the uses clause then
just do the below procedure, but when I
am in the IDE the exception still trips.
Shrug.  When I run the progam my thing captures
the exceptions and handles them..

:)

---code---
procedure TForm1.ConnectClick(Sender: TObject);
var b : integer;
begin
    For b := 0 to servers.count-1 do begin
    ConnectErr := False;
    IdNNTP1.Host:=servers[b];
    IdNNTP1.Port := 119;
    Try
      ListBox1.Items.Add('---------------------');
      ListBox1.Items.Add('Connectting to: '+servers[b]);
      IdNNTP1.Connect(1000);
      ListBox1.Items.Add('-Connectted');
      ListBox1.Items.Add('--Listting Group');
      IdNNTP1.SelectGroup('atozedsoftware.indy.protocol.nntp');
      ListBox1.Items.Add('---Message Count: '+IntToStr(idNNTP1.MsgCount));
      ListBox1.Items.Add('---Message Hi: '+IntToStr(idNNTP1.MsgHigh));
      ListBox1.Items.Add('---Message Low: '+IntToStr(idNNTP1.MsgLow));
      ListBox1.Items.Add('Disconnectted');
      ListBox1.Items.Add('---------------------');
      IdNNTP1.Disconnect;
    Except
      On EIdProtocolReplyError do begin
          ListBox1.Items.Add('-Exception Found - Group not found..');
          ListBox1.Items.Add('Closing Connection');
          ListBox1.Items.Add('---------------------');
          IdNNTP1.Disconnect;
      end;
      on EIdSocketError do begin
          ListBox1.Items.Add('-Exception Found - Can not connect..');
          ListBox1.Items.Add('Closing Connection');
          ListBox1.Items.Add('---------------------');
          connecterr := true;
      end;
    end;
    end;

end;

Replies

In response to

try.. except posted by bluewolf on Sun, 9 Nov 2003