EndConnect Exception

I am trying to connect to the server using BeginConnect and when I specified the wrong IpAddress or Port, I get a SocketException.

The problem is my try / catch is not catching 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 VS method, it informs me of the exception and blocks the program ...

How can I handle this?

Thanks Federico

+2


source to share


1 answer


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



If you fix it (uncheck the "thrown" checkbox for this exception) and it should let your Catch handler execute.

0


source







All Articles