views:

131

answers:

1

Hi, I'm trying to connect to a server using BeginConnect and when I specified an incorrect IpAddress or Port I get a SocketException.

The problem is that my try/catch doesn't catch the Exception:

private void OnConnect(IAsyncResult result)
{
    try
    {
        socket.EndConnect(result);

        status = Status.WaitAck;

        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, onDataReady, null);
    }
    catch (Exception ex)
    {
        if (ConnectionError != null)
            ConnectionError(this, new OpenWebNetErrorEventArgs(ex));
    }
}

When I call the socket.EndConnect method VS report me the exception and block the program...

How can I handle it?

Thanks Federico

A: 

Are you running the program under debugger? If so, you either have VS to break on any exception being thrown, or break on the SocketException being thrown.

If you fix that (uncheck the "thrown" column for that exception) and it should allow your Catch handler to execute.

feroze
Thanks so much :)
Federico Degrandis