Why is Connect() located before "try" instead of in it?

Giganews Newsgroups
Subject: Why is Connect() located before "try" instead of in it?
Posted by:  Vincent Delporte (justa…@acme.com)
Date: Fri, 07 Jul 2006

Hello

    I'm reading up on using the Indy component, and one thing that
still puzzles me with exceptions in the examples in the "Indy in
depth" book (along with other examples in other books), is why Connect
is located _before_ the "try" section instead of _in_ it?

I would expect the tentative instructions to all be located between
the try and except statements, and Connect is an obvious case for
things that can go wrong (same thing for resources reserved with
.Create) .

Here's an example:
--------
  //Also added EIdSocketError in list of errors to ignore by IDE    
  uses IdException;
    
  IdTCPClient1.Host := 'localhost';
  IdTCPClient1.Port := 80;
  IdTCPClient1.Connect;
  try
      try
          ShowMessage('try');
      finally
          IdTCPClient1.Disconnect;
      end;
  except
      ShowMessage('except');
  end;
------------

When I run this and make sure I can't connect (for testing purposes)
and also u, a dialog pops up when running Connect, ie. I don't get to
the except block where I would see "except". To do this, I must move
Connect in the try block.

=> Why do experts recommend doing things this way?

Thank you.

PS : BTW, I'll piggy-back on this thread: Did Delphi 2006 introduce
the try/except/finally structure? To me, that would be a good enough
reason to switch to it :-)

Replies