Re: function ToBytes(Short) @ IdGlobal.pas is wrong.

Giganews Newsgroups
Subject: Re: function ToBytes(Short) @ IdGlobal.pas is wrong.
Posted by:  Remy Lebeau (TeamB) (no.spam@no.spam.com)
Date: Tue, 28 Jun 2005

"Flier Lu" <flier.…@gmail.com> wrote in message
news:81C52FE965D0E240flier.…@gmail.com...

> Hello All, this is my first post in this group.

Your message subject has nothing to do with your message body.  Please
choose more appropriate message subjects.

> When Indy Raises an exception, in most or all of the units,
> my application crashes with an access violation.

Then your code is not handling the exception properly.

>      Connect(3000);//3 second timeout
>      if connected then SelectGroup(NewsGroup) else Sleep(250);

You do not need to check the Connected() method.  If Connect() does not
connect, an exception is raised.

>      if pos('No such group',LastCmdResult.Text.Text) > 0 then Abort;

That is not the correct way to handle that situation.  If SelectGroup()
fails, an EIdProtocolReplyError exception is raised.

>  except on e:exception do if e.message <> 'Operation aborted' then Abort;

Rather than checking the exception's message, you should check its actual
class type instead.

> else Abort; end;

Here you are throwing an unhandled exception.  If you do not handle it,
Abort() can terminate the application altogether.  Why are you using Abort()
anyway?  Where is this code actually being called from?

> The problem comes when the SelectGroup procedure raises
> a 'no such group' error. I know for sure the indy component is
> causing the access violations because the compiler highlights
> the 'Raise [put exception here]' lines in the indy source code.

An AccessViolation is an access to invalid memory.  An Exception in general
is not an AccessViolation, nor does an Exception necessarily mean an error,
either.  An Exception is an Exception, a means for code to jump around
easier and be processed more centrally.  IT has nothing to do with errors
persay, although it is commonly used for that but that is not a requirement.

> If I run the .exe outside the compiler I just get an access violation.

Are you sure it is really an AccessViolation?

> I get the same problem with all other indy exceptions; connection
> timed out, connection closed gracefully; they all crash my app with
> an access violation.

There there is something seriously wrong with your own code.

> If I comment out the 'Raise [put exception here]' lines in the indy
> code then the app works much better, but I dont want to be blind
> to exceptions! I want to log them & would rather catch them in my
> except block.

Try this code instead:

    try
        try
            with client do
            begin
                Connect(3000);//3 second timeout
                try
                    SelectGroup(NewsGroup);
                except
                    Disconnect;
                    raise;
                end;
            end;
        except
            on e: Exception do begin
                // log the exception as needed...
                Abort; // why are you using Abort() in the first place ???
            end;
        end;
    finally
        CloseThread;
    end;

Gambit

Replies

None

In response to

function ToBytes(Short) @ IdGlobal.pas is wrong. posted by Flier Lu on Tue, 28 Jun 2005