Why is this SocketException not caught by a generic catch routine?

Posted by Tarnschaf on Stack Overflow See other posts from Stack Overflow or by Tarnschaf
Published on 2010-02-08T15:29:47Z Indexed on 2010/05/12 19:04 UTC
Read the original article Hit count: 307

Filed under:
|
|

Our company provides a network component (DLL) for a GUI application.

It uses a Timer that checks for disconnections. If it wants to reconnect, it calls:

internal void timClock_TimerCallback(object state)
{
  lock (someLock)
  {
    // ...
    try
    {
         DoConnect();
    }
    catch (Exception e)
    {
        // Log e.Message omitted
        // Raise event with e as parameter
        ErrorEvent(this, new ErrorEventArgs(e));
        DoDisconnect();
    }
    // ...
  }
}

So the problem is, inside of the DoConnect() routine a SocketException is thrown (and not caught). I would assume, that the catch (Exception e) should catch ALL exceptions but somehow the SocketException was not caught and shows up to the GUI application.

protected void DoConnect()
{
    //
    client = new TcpClient();
    client.NoDelay = true;
    // In the following call the SocketException is thrown
    client.Connect(endPoint.Address.ToString(), endPoint.Port);
    // ... (login stuff)
}

The doc confirmed that SocketException extends Exception. The stacktrace that showed up is:

TcpClient.Connect() -> DoConnect() -> timClock_TimerCallback

So the exception is not thrown outside the try/catch block.

Any ideas why it doesn't work?

© Stack Overflow or respective owner

Related posts about c#

Related posts about socketexception