0

This is the code I use to setup my TCP server

    internal void Initialize(int port,string IP)
    {
        IPEndPoint _Point = new IPEndPoint(IPAddress.Parse(IP), port);
        Socket _Accpt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try
        {
            _Accpt.Bind(_Point);
        }
        catch (SocketException exc)
        {
            System.Windows.Forms.MessageBox.Show(exc.Message);

        }
        finally
        {
            _Accpt.Listen(2); //Second exception is here after the code continues after the catch block
            _Accpt.BeginAccept(null, 0, new AsyncCallback(Accept), _Accpt);
        }
    }

If you call Bind on the same destination you'll get an exception,because the port is already in use,so do I get that exception when I call that function two times.

Problem - After Catch{} statement the code continues to follow the Finally{} even though I caught the exception,why that happens? I want it to exit the function after the messagebox.I tried with "return",but it still continues to follow the finally{} block.

6 Answers 6

3

The finally block always executes, regardless if an exception was thrown or the method was exiting from within the try/catch block.

2

The Finally block is where you put code that MUST run regardless of whether the try block succeeds or fails. It's where you'd put "clean up" code that might dispose objects etc.

So, it's by-design that this code runs regardless of what happened. Perhaps you need to move that code into the Try block if you only want it running when the Bind is good.

Check out this page...

http://msdn.microsoft.com/en-us/library/6dekhbbc(VS.80).aspx

... for details on how this works.

A sample try/catch/finally follows (taken from Jeffery Richter's CLR via C# which should be on your required reading)...

FileStream fs = null;

try
{
  fs = new FileStream(...)

  // process the data

}
catch (IOException)
{
  // inside this catch block is where you put code that recovers
  // from an IOException
}
finally
{
  // make sure the file gets closed
  if (fs != null) fs.Close();
}
2

As others have pointed out, the finally block will always occur regardless of an exception being thrown.

Change your code to

    try
    {
        _Accpt.Bind(_Point);
        _Accpt.Listen(2); //Second exception is here after the code continues after the catch block
        _Accpt.BeginAccept(null, 0, new AsyncCallback(Accept), _Accpt);
    }
    catch (SocketException exc)
    {
        System.Windows.Forms.MessageBox.Show(exc.Message);

    }
    finally
    {
        //Final logging
        //Disposal of initial objects etc...
    }
1

The whole idea with finally is that it will always run - exceptions or no exceptions. Use it for clean-up etc.

1

Finally blocks are always executed. That is the whole point of finally.

It sounds like the lines you have in the 'finally' maybe belong in the try?

1

finally is always executed.

Only the code that exists after the line of code that throwed the exception will not be executed, until a catch exception is encountered that can catch the exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.