views:

202

answers:

4

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?

+1  A: 

If ErrorEvent really raises another exception (per the comment), then DoDisconnect() is never executed.

Otherwise, the exception you see might be coming form DoDisconnect()

Henk Holterman
see the added stacktrace why this can't be the solution
Tarnschaf
A: 

Could you post the DoConnect() code?

Also things to try: Can you catch it in the DoConnect()? Try catching the specific exception instead of just the generic. How does it react if you use debug mode?

Lily
Reading the replies you left to others, leave your code as is but add a SECOND catch block to catch either a SocketExeption or a Win32Exception.
Lily
added the DoConnect() method as requested. Do you also have an explanation for your idea as SocketException extends Win32Exception extends ExternalException extends Exception ?
Tarnschaf
There are two reasons, .NET 1.1 win32exception wasn't derived from Exception. Otherwise, your exception isn't caught likely because it is thrown on a different thread started inside DoConnect.
Lily
so you say TcpClient.Connect() surrounded with a generic try/catch will not always catch all exceptions the call may produce?
Tarnschaf
and can you prove the .net 1.1 inheritance? docu says otherwise..
Tarnschaf
A: 

Your timClock_TimerCallback isn't called in the same thread as the catch-statement wants to catch an exception. You should catch the exception inside timClock_TimerCallback and then call a method which invokes itself and then rethrow the exception in the right thread.

Not sure this will work, but you could give it a try.

J. Random Coder
That sounds interesting.. but the TcpClient.Connect() is called from the TimerCallback thread where also the catch resides, which other thread could be involved?
Tarnschaf
Oops, I thougt the timClock_TimerCallback was somehow called in DoConnect, but thats obviously wrong. I'm afraid my answer won't help you.
J. Random Coder
A: 

I wrote a little program and was unable to reproduce, a SocketException was caught inside a TimerCallback just fine.

So I suggest you re-think your analysis, the problem may not be what you think it is. A few suggestions:

  • run it outside the Timer. T|hat takes the threading out of the loop.
  • run it in the debugger. Where does the exception really occur?
  • step through the exception handling. Is ErrorEvent doing what it should?
Henk Holterman
Thank you, I asked the question because I also had no explanation why this happened and was hoping for new inspiration. I don't think it's reproducable (as your program showed) but it obviously happened, so I had to investigate..
Tarnschaf
The behaviour is still not reproducable, so I added debug code and will wait if it occurs again. Accepting your answer because it is most straightforward.
Tarnschaf